Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#6361 closed Uncategorized (wontfix)

Models Documentation Default Choice

Reported by: Rupert Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX:

Description

The documentation for the 'default' field attribute should include information on setting the default when a choices tuple has been used. Something like:

When using a tuple or list of choices enter the index of the choice that you would like to be default. e.g. default=0 would make the first choice the default.

Just though I'd suggest this to improve the already superb documentation.

Attachments (0)

Change History (4)

comment:1 Changed 6 years ago by Simon Greenhill <dev@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by ubernostrum

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

I'm not sure why there needs to be special documentation for this, as opposed to all the other fields that take a default argument...

comment:3 follow-up: Changed 3 years ago by anonymous

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized

I've only found how to make default choice for my choice field by reading this ticket.

comment:4 in reply to: ↑ 3 Changed 3 years ago by anonymous

Replying to anonymous:

I've only found how to make default choice for my choice field by reading this ticket.

Hmm, you might want to know then that the ticket description is wrong. Setting a default of 0 gives that as the default: 0. Not the value associated with the 1st element in the choices list. For example with this model:

class Thingy(models.Model):
    ttype = models.CharField(max_length=20, choices=(('abc', 'ABC!'), ('123', '123!')), default=0)

you get a Thingy with ttype value 0, not 'abc', when you create a Thingy and don't specify ttype:

>>> from ttt.models import Thingy
>>> newt = Thingy.objects.create()
>>> newt.ttype
0
>>>

There is nothing special about specifying the default value for a field that has choices specified. You need to specify just that: the default value you want the field to have, just as you do for fields without choices. choices are orthoganal and used for form presentation purposes only; there is no consideration of the choices value when the default value is recorded or used.

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.