Tuesday, November 30, 2010

Re: Choices vs. ForeignKeys (was: Django - Alternative to using NULLs? (for integer and FK fields).)

On Tuesday 30 November 2010 08:53:49 Todd Wilson wrote:
> Mike Dewhirst wrote, on 11/29/2010 10:33 PM:
> > I'm keeping track of companies, divisions and people with their
> > relationships. For example, divisions can be traded between companies
> > and people consult to companies or own trading entities. I can also keep
> > track of pretty much any relationship of interest.
> >
> > Hope this helps ...
> >
> > class Entity(models.Model):
> > """
> > Entities can be corporations or humans. entity_type indicates
> > which.
> > """
> > entity_type = models.CharField(max_length=MEDIUM, blank=False,
> >
> > choices=ENTITY_TYPES,
> > default=ENTITY_TYPES[0][0])
>
> Although this is not directly related to the question that started this
> thread, your example raises a question that I've had as I've read the
> documentation. Instead of hard-coding the entity types here, you are
> using a constant, presumably because you may want to introduce more
> entity types later. But what are the trade-offs bewteen representing
> types as CharFields with choices, as you are doing here, versus a
> separate table of types to which this model has a foreign-key
> relationship? I'm facing this decision in a number of different places
> in a Django application I'm working on.

I try to answer this one since I spent good deal of my precious time to think
the exactly same thing while back.

I rarely use strings for actual values. It's just for preformance point of
view. It makes database easy to read but if you do constant searches by some
field you want to index it. And strings don't index well.

So it's also possible, and personally I opt for to use type-kind stuff as
integer field.

class MyMode(models.Model)
SOME_CHOICES = ((1, 'Choice 1'),
(2, 'Choice 2'))
my_field = models.IntegerField(choices=SOME_CHOICES, default=1)

to get visible representation of my_field you can do using built-in feature of
Django ORM:

instance_of_mymodel.get_my_field_display()

But when to use choices and when fk?

I use choices is when I have a relatively fixed set of options that basically
don't change ever. It's just that adding new stuff involves so much including
deployment to customer environments.

Foreign key I use when ever data is meant to be changed or edited by end user
or it contains much more data than simple textual value.

And as known relational databases are meant to handle um.. relational data.
And they do that very well. So it's not much of a problem to add models for
that kind of a data.

--

Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment