Scott
Scott

Reputation: 3344

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 before the school, how do I give 'school' a default empty value? Is it blank or null or what?

Thanks!

Upvotes: 7

Views: 5657

Answers (1)

synic
synic

Reputation: 26678

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.

Upvotes: 9

Related Questions