Django: Help using ModelChoiceField - django

Im new to Django and currently building an application for logging and lending of various equipment.
I have created multiple instances of my class Equipment in the database and I want to display these in my view, preferable in a list. Additionally I would like the option to select the list objects, one at a time.
As I understand, "ModelChoiceField" gets the job done, but I just don't understand how to implement it. I've read the specific documentation, but yet I've been failing for many hours and I feel really stupid.
So if someone would be so kind to give me a generic example of how to implement ModelChoiceField throughout veiws.py, forms.py, templates.html etc. I'd be very grateful!

forms.py:
class MyForm(forms.Form):
# or with some filter applied
my_field = forms.ModelChoiceField(queryset=Equipment.objects.all())
Then just use {{ form.as_<whatever you want> }} in your template and it should work.

Related

writing a generic listview template to use across different models

I am new to django and am looking for a generic way of displaying all records of a model in a view by writing minimum html.
So ideally what i would like to do is define my model for example customer, add fields like first name, address, credit card no.
Now i would mark which of these fields are to be rendered ( say publicly_visible = false for credit card field). I repeat this for another model like 'products'.
Next i want my view for customer to render a list of all customer records (the credit card column will not be rendered).
I am wondering if there already is a django package which will do this for me?
The other option which i am trying is to try to use a generic listview to do this but not sure how to proceed.
Meet django's "Class Based Views".
You can read the docs here Django Docs on CBV, there are several generic views to accomplish repetitve tasks.
The view you're looking for regarding your question is called ListView.
For others looking for a good solution to generate read-only list views for your models without writing too much code - This did it for me.
https://github.com/miracle2k/django-tables

User-based views in Django 1.6

This is probably a very newbie question however I'm kind of stuck.
I've been looking around for a way to render a user based view for a calendar scheduler however I can't seem to find anything about user-based views in django. It's probably really simple but I'm too blind to spot any documentation about it. Something alongside of a user profile.
I've read through the django book but I cannot recall if there was anything that describes what I'm trying to achieve. Any hints or links would be a great help.
Thank you.
What you are actually are looking for is a DetailView, Django nowdays uses Class Based Views which are kind of self explanatory, there is a ListView which is used to display a list of objects, a DetailView which is used to display details of a specific object and others like FormViews, CreateView, DeleteView etc (you can find more information to this very detailed site apart from Django's own documentation):
http://ccbv.co.uk/
Since you want to display a view for a specified object, this becomes a DetailView, since a User also has a calendar with events, then you also need to fetch the Calendar with the events (but this all breaks down to how your model relations are glued together).
For instance if your user has a Single Calendar (he...should actually) and many events in the calendar, you could declare a Queryset in the DetailView:
http://ccbv.co.uk/projects/Django/1.6/django.views.generic.detail/DetailView/
You can create common template for all users and then populate it with user specific data.
I'm not really sure where you're stuck, or what you're defining as a "user-based view".
The user model is like any other: you can query it, and other models can be related to it. If you just want to show data for the current user, you get that from request.user: otherwise, you can pass the user ID in the URL like any other parameter.

Use a form inside a form or How to work with foreign keys in forms

I am going to use the documentation model as an example:
class Car(models.Model):
manufacturer = models.ForeignKey('Manufacturer')
# ...
class Manufacturer(models.Model):
# ...
Let's say I want to create a form to add a new Manufacturer, and in this form I want to be able to add new cars. How would it be done with django-forms?
Is it even possible?
Thank you in advance for your help!
The short answer:
You want modelformset_factory, documented here: http://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#model-formsets
The still-rather-short answer, but with a couple of gotchas to watch for:
On the processing side, if you're creating both the Manufacturer and the multiple Car instances, you'll want to be sure to save the manufacturer first, before saving the individual cars (which must reference the manufacturer). Make sure this happens in a database transaction if you can help it.
One more note: if this is a bit confusing to you, beat in mind that there's no hard and fast rule saying that you can only process one form in a request. You just have multiple forms.Form (or subclasses thereof) objects within the HTML <form> tag, which posts to a single request location that processes each form individually and saves them out. Again, use a database transaction so that if something fails at the end, the entire thing gets rolled back and the user can correct their error without having bad or orphan data in the database.

Django: Display existing image with an instantiated ModelForm, rather than a file input?

I have some images assigned to an ImageField on several saved model instances. I want to display a ModelForm based on the model of interest, only for the purpose of setting one foreign key on the model. I'm instantiating the ModelForm with the model I want to edit.
All I'd like to do is display the existing image for the model along with the FK select. It would be easy enough to do both separately, but as I am actually displaying multiple forms side by side, within a {% for form in forms %} loop, I want to get the image displayed using something along the lines of <img src="{{ form.image.get_image_url }} />. I don't believe any template syntax like this exists for ImageField's on forms, but hopefully you catch my drift.
This seems like it should be a trivial exercise, but I haven't found a clear answer in the documentation or elsewhere. There was a suggestion on some forum threads that creating a custom field Widget might be the recommended solution, mostly with regards to customizing the admin, but I can't sort out the details of exactly how I should do this in my case on the site front-end. Thanks in advance for any hints.
You can access the model object associated with a ModelForm (provided such an association exists) using form.instance. From there you can access the image as you normally do.

Django: One FormWizard for multiple models

Would it be possible/best practice to use 1 FormWizard for manipulating multiple models?
I've experimented with the FormWizard and have defined all the forms. The page 'flow' itself works like a charm. However with all the checks that need to be done and Models that are manipulated it feels like I'm sticking code in the form's __init__ and/or process_step() that really belongs in views.py. The docs even state: "Dont manipulate form data via process_step().
Testing a concept with everything in one view, thus using a last_submitted_page (the step equiv) it feels like I'm writing another formwizard.
Anybody been here before? Tips welcome.
Thanx a lot.
Regards,
Gerard.
Zooming out on the issue I decided to go for the FormWizard approach. I re-confirmed that code is easily placed in forms.py and that you can get your model info by using. the process_step().
GrtzG