Code

Opened 5 years ago

Closed 5 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: UI/UX:

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.

Attachments (0)

Change History (2)

comment:1 Changed 5 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 5 years ago by olimination

  • Resolution set to wontfix
  • Status changed from new to closed

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!

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.