Opened 15 years ago

Closed 15 years ago

#10085 closed (wontfix)

Forms from model documentation: The importance of the "_unicode_" method of a model

Reported by: olimination Owned by: nobody
Component: Documentation Version: 1.0
Severity: Keywords: forms model
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Though you note in the "First steps"-tutorial that the "_unicode-" method is a quite often used method for the Django admin,etc. it would be nice, if you document this in the "Forms from model" documentation too, because the "ModelChoiceField" is using the "_unicode_" method for showing the model items in the html-select-field. If you don't provide this method, the html-select-field will be emtpy. No warnings or similar is showed by Django, its then a bit confusing for a newbie like me :-).

For a newbie like me, it took almost a day to figure this out, because no error/warning have be thrown by Django.

I think it will be helpful for other starting newbies that this behaviour should be explicit documented for the forms documentation.

Change History (2)

comment:1 by Karen Tracey, 15 years ago

I am not sure where you think this mention will be helpful? The doc for ModelChoiceField already notes that __unicode__ is what is used to populate the select box dropdown list:

http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield

Also I'm puzzled by your experience with neglecting to include a __unicode__ method causing nothing to be displayed. If I remove the __unicode__ method from a model (Authors) and then go to the admin for a related model that has an Authors ForeignKey, I see a drop-down list that is populated with "Authors object" entries, not an empty select box. So more information on what you had, exactly (and what exact level of Django you are running), might help track down what caused an empty select box in your case. I think it had to be more than lack of a __unicode__ method.

comment:2 by olimination, 15 years ago

Resolution: wontfix
Status: newclosed

Your posted link was exactly that, what I had needed at that time, I just haven't found it :-).
I have coded a small application for administrating persons, grades and modules for an university of applied sciences.
I had created a PersonForm class which had inherited from the forms.ModelForm and which had a Location-object as a foreignkey. I had forgotten then to set the _unicode_ method in the Location-model. In the template then, the html-select-field was empty and no errors or warnings occured.
If you are interested to see the code: http://svn.origo.ethz.ch/wsvn/sunas/branches/0.9-beta/src/sunas/persons/models.py (but since then we have changed already a lot..).

I think you can close the ticket, because the stuff is documented.

Thanks!

Note: See TracTickets for help on using tickets.
Back to Top