Code

Opened 5 years ago

Closed 5 years ago

#11548 closed (invalid)

choices in models.CharField accepts method but no property

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

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

Attachments (0)

Change History (1)

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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

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.