tags:

views:

21

answers:

1

A question in the proper use of a choice-based field in Django relative to searchability and speed. If I design my model like so:

STATUS_CHOICES = ((1, 'Published'),
                  (2, 'Unpublished'),
                  (3, 'Retired'),)

status = IntegerField('Status', choices=STATUS_CHOICES)

In my database I do not have the actual status' saved - only a key relative to a value at the Python/Django level. If in understand correctly, this means that full-text search with something like Whoosh will make this field meaningless - it'll only have the key and not the value. Is this true? Would a CharField with something like the following be better:

STATUS_CHOICES = (('Published', 'Published'),
                  ('Unpublished', 'Unpublished'),
                  ('Retired', 'Retired'),)

Or would it be better to do this as a ForeignKey, which I had always heard was much slower (since it's executing a join at the database level)?

A: 

Don't use a ForeignKey unless those choices need to change regularly or after your code is deployed.

Your second option is the correct way to do this. Using cryptic magic numbers in your database won't make things particularly faster, and it makes your life harder later if you need to change things around.

The choices data structure is specifically designed to allow you to display an even more verbose set of options to users than you want to store in your DB. You might store "published" in the db, but display to the user "Work Published" or something.

Paul McMillan