添加用户外键时迁移模型

时间:2015-07-14 18:04:25

标签: django python-3.x

所以我有这个模型,我将用户字段添加为用户模型的外键

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings

class Post(models.Model):
    url = models.URLField()
    title = models.CharField(max_length=140)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

比我跑

python3 manage.py makemigrations

得到:

You are trying to add a non-nullable field 'user' to comment without a    default; 
we can't do that (the database needs something to populate existing rows).
Please select a fix:
    1) Provide a one-off default now (will be set on all existing rows)
    2) Quit, and let me add a default in models.py

我知道我需要给它默认用户,但我不知道怎么做。

P.S。 Django版本 - 1.8.3

1 个答案:

答案 0 :(得分:0)

好的,所以迁移实用程序的问题对我来说似乎不够直观。迁移实用程序真正要求的是User模型的必填字段。所以基本上你需要输入一些数据3次(用户名,密码和电子邮件)。问题是效用并不是真正说出它所期望的领域。

相关问题