Monday, November 29, 2010

Django - Alternative to using NULLs? (for integer and FK fields).

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

What about in the case of optional foreign keys (spouse and children)
- is there a better way of handling these, without using NULLs?

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