Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#972 closed defect (fixed)

better explain choices usage in the model docs

Reported by: radek Owned by: jacob
Component: Documentation Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


When using choices option as documented in
it is important to define the choices outside of the class definition (not inside). In the wrong way validation raises error.


class Website(meta.Model):

    type = meta.CharField('Type', maxlength=200, blank = True, choices=TYPE_CHOICES)



class Website(meta.Model):
    type = meta.CharField('Type', maxlength=200, blank = True, choices=TYPE_CHOICES)

Please, improve docs correspondingly.

Change History (3)

comment:1 Changed 10 years ago by adrian

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

This is incorrect. You can define CHOICES either inside or outside the field definition. I'm not sure which validation error you got, but it had to have been unrelated.

comment:2 Changed 10 years ago by GrumpySimon

  • Resolution invalid deleted
  • Status changed from closed to reopened

I get the same error ( latest svn version - 1565 ):

class Word( meta.Model ):
        ( 0, 'Not Done' ),
        ( 1, 'Preliminary' ),
        ( 2, 'Checked' ),

    word = meta.CharField( maxlength = 32 )
    slug = meta.SlugField( )
    state = meta.SmallIntegerField( choices = COGNACY_STATES )

gives this traceback:

Traceback (most recent call last):
  File "/usr/local/bin/", line 5, in ?
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/core/", line 903, in execute_from_command_line
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/core/", line 505, in createsuperuser
    from django.models.auth import users
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/models/", line 13, in ?
    modules = meta.get_installed_model_modules(__all__)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/core/meta/", line 111, in get_installed_model_modules
    mod = __import__('django.models.%s' % submodule, '', '', [''])
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/language/apps/austronesian/models/", line 21, in ?
    class Word( meta.Model ):
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/core/meta/", line 699, in __new__
    assert callable(v), "%r is an invalid model parameter." % k
AssertionError: 'COGNACY_STATES' is an invalid model parameter.

whilst moving COGNACY_STATES outside of the class seems to work ok.


comment:3 Changed 10 years ago by adrian

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [1570]) Fixed #972 -- Improved docs for 'choices' model field option. Thanks, radek

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