开发者

Django Database Design - Null for

Say I have the following model:

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.Int开发者_如何学CegerField(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) 

Several of the above fields are meant to be optional (e.g. street_address, home_phone_number and work_phone_number, as well as spouse/children).

In Django, for CharField fields, you can set "blank=True" and if the user leaves the form field blank, Django will store a empty string. That's fine.

However, for integer fields like home_phone_number and work_phone_number, this doesn't work - I've had to use "null=True" in order to deal with the case of people not submitting in those details. Is there a better way, model/database design wise to deal with this, and not have NULLs?

Finally, for optional foreign keys like spouse/children (a person may not be married, or may not have any kids), how should you handle those?

Cheers, Victor


(A) Phone numbers aren't actually integers, they are character strings, and should be stored in the database as such.

(B) Null is basically the correct way to optional foreign keys.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜