On Tue, Nov 30, 2010 at 4:11 AM, Lachlan Musicman <data...@gmail.com> wrote: > On Tue, Nov 30, 2010 at 12:28, Victor Hooi <victorh...@gmail.com> wrote: >> Hi, >> >> I'm wondering what the community's stance on using NULL in Django is? >> >> Say for example you have: >> >> class Person(models.Model): >> street_address = models.CharField(max_length=50, blank=True) >> suburb = models.CharField(max_length=30) >> postcode = models.IntegerField() >> state = models.CharField(max_length=3) >> email = models.EmailField() >> mobile_phone_number = models.IntegerField(max_length=12) >> home_phone_number = models.IntegerField(max_length=10, >> null=True, blank=True) >> work_phone_number = models.IntegerField(max_length=8, >> null=True, blank=True) >> >> spouse = models.ForeignKey('self', null=True, blank=True) >> children = models.ManyToManyField('self', null=True, >> blank=True) >> >> For string fields like street_address, I can make these "blank=True", >> and Django will store an empty string if the user leaves it blank. >> >> However, for integer fields like home_phone_number and >> work_phone_number, I've had to make these "null=True" for the case >> where somebody doesn't supply them (i.e. they're meant to be optional, >> mobile is required). >> >> However, is there a better way of handling this case? (assuming I want >> to keep these fields as integers). > > > 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
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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.