views:

119

answers:

1

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!

+3  A: 

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.

synic
Yup, I normally set an FK to both if it is optional.
T. Stone
Ok that works and I had tried it before. I'd run manage.py flush but evidently that isn't good enough to update the db. The only way to get it to change was deleting the db file and running manage.py syncdb.
Scott Willman
syncdb doesn't change tables, it only creates them. If you want to modify the model, you have to do it manually, or use something like http://south.aeracode.org/
synic