Hi Radomir,

On 12/17/2014 09:56 AM, Radomir Wojcik wrote:
> The official django doc uses this as an example (see below). Is there any 
> point to storing 'FR' instead of 'Freshman" so the choice matches what is 
> stored? Is it faster at all? Saves room? What about for querying the data? 
> Then you have to lookup the symbol 'FR' in the tuple if someone wants to 
> query by 'Freshman'. 
> 
> What is the best practice? Is it even more efficient if integers were used, 
> like 1 value to represent Freshman and so on? To me it makes sense to store 
> 'Freshman' as 'Freshman' but I'd like to get some insight.

I'm no expert on database performance (and what I do know is entirely
specific to Postgres), but I think that a shorter string will be
somewhat more efficient (both in terms of space and speed) than a longer
string, and an integer more efficient than either. However, I'd consider
worrying about that to be premature optimization, unless you have
evidence of it being a problem.

For me the two primary considerations here are:

1) It is useful to separate the database-stored value from the displayed
representation for humans, because the latter is prone to change (in a
sense this is a normalization, so the real name is only stored in one
place, not in various rows throughout your table).

2) I find some ease-of-development value in using a text slug rather
than an integer for the database-stored value, simply because it makes
database rows more readable when working with the database directly.

Carl

> from django.db import models
> 
> class Student(models.Model):
>     FRESHMAN = 'FR'
>     SOPHOMORE = 'SO'
>     JUNIOR = 'JR'
>     SENIOR = 'SR'
>     YEAR_IN_SCHOOL_CHOICES = (
>         (FRESHMAN, 'Freshman'),
>         (SOPHOMORE, 'Sophomore'),
>         (JUNIOR, 'Junior'),
>         (SENIOR, 'Senior'),
>     )
>     year_in_school = models.CharField(max_length=2,
>                                       choices=YEAR_IN_SCHOOL_CHOICES,
>                                       default=FRESHMAN)
> 
>     def is_upperclass(self):
>         return self.year_in_school in (self.JUNIOR, self.SENIOR)
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5491B9D3.3020706%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to