NOT NULL约束失败:login_voterlist.Voter_id

时间:2019-06-10 17:27:11

标签: python-3.x

我尝试过null=True, blank=True,进行了迁移,但没有帮助

**models.py**
class Voterlist(models.Model):
Voter=models.OneToOneField(User,on_delete=models.CASCADE,default=None,null=True)
id=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID',blank=True)
Phone=models.CharField(max_length=14)
Date_of_Birth=models.CharField(max_length=20)
Address=models.CharField(max_length=300)
Gender=models.CharField(max_length=6)
Province = models.CharField(max_length=50)
Citizen_ID_Number=models.CharField(max_length=50)
Voter_ID_Number=models.CharField(max_length=20,default=random.randint(1000,1000000))
Photo_Candidate=models.ImageField(default='default.png',upload_to='profile_pics')
Photo_Citizenship_Front = models.ImageField(upload_to='ctzn_frnt')
Photo_Citizenship_Rare = models.ImageField(upload_to='ctzn_rer')
def __str__(self):
    return self.Citizen_ID_Number

form.py

class UserCreationForm(UserCreationForm):
first_name = forms.CharField(max_length=100)
last_name = forms.CharField(max_length=100)
email=forms.EmailField()
class Meta:
    model=User
    # fields=['username','email','password1','password2']
    fields = UserCreationForm.Meta.fields + ('first_name', 'last_name', 'email',)

form.py

class UserRegisterForm(forms.ModelForm):
    class Meta:
        model=Voterlist
        exclude=('Voter_ID_Number',)
        fields=('Date_of_Birth','Address','Phone','Gender',
                'Province','Citizen_ID_Number','Photo_Candidate','Photo_Citizenship_Front','Photo_Citizenship_Rare',)

views.py

def register(request):
flag=""
if request.method=='POST':
    form=UserCreationForm(request.POST)
    print(form.is_valid)
    if form.is_valid():

signals.py

@receiver(post_save,sender=User)
def create_profile(sender,instance,created,**kwargs):
    if created:
        Voterlist.objects.create(Voter=instance)

@receiver(post_save,sender=User)
def save_profile(sender,instance,**kwargs):
    instance.profile.save()

0 个答案:

没有答案