开发者

Django ForeignKey created empty?

This seems very basic and I must be missing something, but here goes anyways...

With two models like so:

class School(models.Model):
    name    = models.CharField("Official School Name", max_length=128)
    address = models.TextField("Address of the School", max_length=256)
    mascot  = models.CharField("Name of the School Mascot", max_length=128)

class StudentProfile(models.Model):
    name   = models.CharField(max_length=128)
    school = models.ForeignKey(School)

If the student gets created befo开发者_如何转开发re the school, how do I give 'school' a default empty value? Is it blank or null or what?

Thanks!


null if you want the database to allow the relationship to be null, blank if you don't want the Django admin site to complain about it being null.

From the documentation on "blank":

"Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, validation on Django’s admin site will allow entry of an empty value. If a field has blank=False, the field will be required."

In short, you probably want both.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜