Opened 15 years ago

Closed 15 years ago

#11548 closed (invalid)

choices in models.CharField accepts method but no property

Reported by: gatwanagu Owned by: nobody
Component: Uncategorized Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Trying to generate the choices list for models.charField dynamically the following problem occurred.

somefield = = models.CharField(
    verbose_name=_('somefield'), 
    max_length="20", 
    choices=config.SOMEFIELD_CHOICES(),
    default=config.SOMEFIELD_DEFAULT,
    help_text=_('Some field.'),
    )

where SOMEFIELD_CHOICES was a method:

def SOMEFIELD_CHOICES():
    """Return subset of tuples as needed by Django"""
    return [(key, u'%s' % value[0]) for (key, value) in _SOMEFIELD_CHOICES.iteritems()]

is accepted by the model check, while doing it like

somefield = = models.CharField(
    verbose_name=_('somefield'), 
    max_length="20", 
    choices=config.SOMEFIELD_CHOICES,
    default=config.SOMEFIELD_DEFAULT,
    help_text=_('Some field.'),
    )

where SOMEFIELD_CHOICES is a property:

@property
def SOMEFIELD_CHOICES(:
    """Return subset of tuples as needed by Django"""
    return [(key, u'%s' % value[0]) for (key, value) in _SOMEFIELD_CHOICES.iteritems()]

throws the following error:

Validating models...
Unhandled exception in thread started by <function inner_run at 0x01392B30>
Traceback (most recent call last):
  File "D:\pyt\25\lib\site-packages\django\core\management\commands\runserver.py", line 48, in inner_run
    self.validate(display_num_errors=True)
  File "D:\pyt\25\lib\site-packages\django\core\management\base.py", line 253, in validate
    raise CommandError("One or more models did not validate:\n%s" % error_text)
django.core.management.base.CommandError: One or more models did not validate:
example.example: "somefield": "choices" should be iterable (e.g., a tuple or list).

I feel that Django should also accept the property version.

Rgds
Günter

Change History (1)

comment:1 by Alex Gaynor, 15 years ago

Resolution: invalid
Status: newclosed

Properties can only exist on instances of a class, therefore you're giving Django a property object.

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