Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Use "Python identifier" from three-tuple Choices in StatusModel #25

Open
chronossc opened this Issue Feb 12, 2013 · 4 comments

Comments

Projects
None yet
3 participants
Contributor

chronossc commented Feb 12, 2013

Hello Carl.

I started to use StatusModel this week (amazing tool btw) and with model like that:

class MyModel(StatusModel):
    STATUS = Choices(
        ('0', 'nao_atendido', 'Não atendido'),
        ('10', 'em_atendimento', 'Em atendimento'),
        ('11', 'contato_sem_sucesso', 'Contato sem sucesso'),
        ('20', 'atendido_sem_sucesso', 'Atendido sem sucesso'),
        ('21', 'atendido_com_sucesso', 'Atendido com sucesso')
    )

If I do:

>>> x = MyModel(status=MyModel.STATUS.nao_atendido)
>>> x.status
u'0'
>>> x.0
x.0
  ^
SyntaxError: invalid syntax

Same happens with a saved instance.

So, IMO, in case of tree tuple choice, it should use second item instead first item to create managers, and, never use a digit.

I love to write patches for you, but since I'm not needing this feature right now and without time I just created that issue.

Collaborator

carljm commented Feb 12, 2013

Yes, I agree that in case of three-tuple Choices, StatusModel ought to use the second item in each tuple, not the first, to create the status managers.

I don't use StatusModel at the moment, so I'm not likely to write a patch for this, but it shouldn't be a hard patch for anyone who wants to make a contribution.

Contributor

chronossc commented Feb 13, 2013

Well, if you agree, I do it when have sometime and send a pull request. I may need to use that feature next week.

Thx!

morenopc commented Aug 5, 2013

I found a similar issue when the first choice character is a number (django-model-utils == 1.4.0)

In [1]: from model_utils import Choices

In [2]: DELAY = Choices(
    ('3h', '3h'),
    ('12h', '12h'),
    ('48h', '48h')
)

In [3]: DELAY.3h
  File "<ipython-input-4-4ef4b0a6edb7>", line 1
    DELAY.3h
          ^
SyntaxError: invalid syntax

In [4]: DELAY = Choices(('3h'), ('12h'), ('48h'))

In [5]: DELAY.3h
  File "<ipython-input-6-f8586051cbcd>", line 1
    DELAY.3h
           ^
SyntaxError: invalid syntax
Collaborator

carljm commented Aug 6, 2013

@morenopc There's not really anything model-utils can do about that situation; when your desired database representation is not a valid Python identifier, you should use the triple form instead. A string that begins with a number is not a valid Python identifier.

kezabelle added a commit to kezabelle/django-model-utils that referenced this issue Nov 24, 2013

Fixed #25 - If using a Choices instance for a StatusModel
always pull the data from the identifier map, which
ensures the python identifier form is used,
if using the 3-value tuple form.

Thanks to Felipe Prenholato for reporting the issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment