如何在迁移步骤中修复OnetoOneField中的唯一约束

时间:2019-02-09 13:39:01

标签: python django

我不明白为什么每次唯一约束都会失败。 #models.py

from django.db import models
# Create your models here.

class usersclass(models.Model):    
    user =  models.OneToOneField(
        'auth.User',
        default = False,
        on_delete = models.CASCADE,
        related_name = 'profiles',
    )


I am using Meta class for including Fields

#forms.py

from django import forms
from .models import usersclass
from django.contrib.auth.models import User

class usersclassForm(forms.ModelForm):
    """Form definition for userclass."""
    class Meta:
        """Meta definition for userclassform."""
        model = usersclass
        fields = ('name_full','address','Gender','timestamp','Mobile_num','landline_number')

身份验证已正确完成,但我无法迁移模型     #veiws.py

    def post(self,request,*args,**kwargs):
        form = LoginForm(request.POST)
        if form.is_valid():
            user = authenticate(
                username = form.cleaned_data['Name'],
                password = form.cleaned_data['password'],
            )
            if user is not None:
                login(request,user)
                return HttpResponse()

1 个答案:

答案 0 :(得分:0)

请勿执行default=False并给出unique=True,然后运行makemigrations并进行迁移以添加唯一约束。