Is it possible to know why you would want to keep them as integers?
Given that there are no mathematical functions that you would want to
apply to them....
> What about in the case of optional foreign keys (spouse and children)
> - is there a better way of handling these, without using NULLs?
As I understand it, foreign keys are kept in the db as follows:
1. table_Person
2. table_Person_children
3. table_Person_spouse
table 2 has three columns: id, Person, Children
table 3 has three columns: id, Person, Spouse
or something to that effect.
Therefore, if there is no Spouse or Child, there is no entry for
Person in tables 2 or 3.
> Cheers,
> Victor
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>
Victor
I'm coming in late on this and don't have the context for your design
but I think there might be a better (perhaps more flexible) way to
handle spouses and children without worrying about NULLs.
I really like a single table for everyone. After all spouses and
children are persons too. You can use a separate table to hold named
many-to-many relationships between the person table and itself.
If the relationship is "Spouse" then that relationship speaks for
itself. Children can simultaneously have relationships with "Father",
"Mother", "Step-mother" etc. Other persons can have "Ex-spouse"
relationships when divorced etc.
If you can find any person then you can navigate through all the
relationships to find all connected persons.
Finally, if someone has multiple spouses then they probably need
counselling but at least you can represent it with multiple relationship
records :)
Mike
Victor
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])
entity_name = models.CharField(max_length=LARGE, blank=False)
entity_title = models.CharField(max_length=SMALL, blank=True)
other_name = models.CharField(max_length=LARGE, blank=True)
slug = models.SlugField(max_length=VLARGE)
updated_by = models.ForeignKey(User, blank=True, null=True)
updated_date = models.DateTimeField(blank=True)
address = models.ForeignKey(Address, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
saved = models.DateTimeField(auto_now=True)
saved_by = models.ForeignKey(User, blank=True, null=True,
related_name='entity_saved_by')
class Meta:
verbose_name_plural = 'entities'
def __unicode__(self):
ename = u' '.join(self.entity_title,
self.other_name,
self.entity_name)
return u'%s: %s (%s)' % (self.pk,
ename.strip(),
self.entity_type)
class Relationship(models.Model):
entity = models.ForeignKey(Entity, null=False,
related_name='rel_entity')
xref = models.ForeignKey(Entity, null=False,
related_name='xref_entity')
relationship = models.CharField(max_length=MEDIUM, blank=False,
choices=RELATIONSHIPS,
default=RELATIONSHIPS[0][0])
comment = models.CharField(max_length=HUGE, blank=True)
start_date = models.DateTimeField(blank=True, null=True)
end_date = models.DateTimeField(blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
saved = models.DateTimeField(auto_now=True)
saved_by = models.ForeignKey(User, blank=True, null=True,
related_name='relationship_saved_by')
def __unicode__(self):
return u'%s: %s ' % (self.relationship, self.xref)
Mike
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
Todd Wilson writes:
> [...]
>
> [...] 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 find choices much easier. Additionally, they behave more nicely
in ModelForms as they create a proper widget automatically. I use
FKs only if I want to attach additional information to the choices.
Tsch�,
Torsten.
--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: torsten...@jabber.rwth-aachen.de
or http://bronger-jmp.appspot.com
You understand it incorrectly. A foreign key on fooapp.FooModelA to
fooapp.FooModelB would be modelled in the database as an
integer/foreign key field (depending on engine) called foomodelb_id on
table fooapp_foomodela.
>
> table 2 has three columns: id, Person, Children
> table 3 has three columns: id, Person, Spouse
>
> or something to that effect.
>
> Therefore, if there is no Spouse or Child, there is no entry for
> Person in tables 2 or 3.
You are describing an m2m relationship, not a foreign key.
Cheers
Tom
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