开发者

Allow null in foreign key to user. Django

I have this model

class Vacancy(models.Model):
    user = models.ForeignKey(User开发者_如何学运维, null=True, blank=True, default = None)
    name = models.CharField(max_length=64)

When in admin i try to creat a vacancy without a user. And it throws an error " club_vacancy.user_id may not be NULL". Am i doing something wrong?


club_vacancy.user_id may not be NULL

Looks very much like an error from your database, rather than from Django.

It seems most likely that you added null=True after running manage.py syncdb. You'll need to modify your database schema to allow null values in that column.


Aside from South, another option is to use django evolution for schema changes.

http://code.google.com/p/django-evolution/

Install it before making db changes. Then run

python manage.py evolve --hint --execute

Make sure that if you add a new field, you allow nulls (null=True) or else evolve will give you an error message.


you need to reset you database table (since just syncdb does not update fields that are alredy created with null=False)

./manage.py reset your_app

OR if there is some data that you do not want to loose use SQL commands to remove NOT NULL flag

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜