Monday, November 29, 2010

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

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.

Thanks,

--Todd

--
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