Todd O'Bryan wrote:
>Your comment at the end got me thinking, though. Writing
>
>trunk.get_branch(kind__exact=2)
>
>is not very illuminating, but you're correct that the value 'Dead'
>could get changed later. In Java, I'd use constants for the integer
>values
>
>public static final int DEAD = 2;
>
>but that seems to violate DRY, because the semantics is already
>listed in the choices list. I like using integers for what end up
>being enumerated types because they don't take much space in the
>database and, as you mentioned, it's easy to change the English
>version without having to do anything to the db representation.
>
>Is there a better way to do this kind of thing?
>
>
This got me thinking too :-)
Generally when I need a constant in Python I don't hesitate to use
string values for constants which are both values and names. So I'd have
BRANCH_KINDS = (('main', 'Main'), ('aux', 'Auxiliary'), ('dead',
'Dead'),)
I think it won't even hurt performance in DB lookups if you create index
for this field. However this implies changing the field to CharField
which won't become a <select> box in admin and in automatic manipulators
(if I'm not mistaken). A lookup table for branch kinds would solve this:
class BranchKind(meta.Model):
id = meta.SlugField(primary_key=True)
title = meta.CharField(maxlength=50)
You can then do something like
trunk.get_branch(pk='dead')
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---