0

在django的后台添加用户的时候爆出如下错误

目测是两个相互关联的外键出错了,但是不知道原因是为什么

from datetime import datetime
from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser


class UserProfile(AbstractUser):
    nick_name= models.CharField(max_length=50,verbose_name=u"昵称",default="")
    birthday=models.DateField(verbose_name=u"生日",null=True,blank=True)
    gender = models.CharField(max_length=6,choices=(("male",u"男"),("female","女")),default="female")
    address =models.CharField(verbose_name=u"地址",max_length=100,default="")
    mobile=models.CharField(max_length=11,null=True,blank=True)
    image=models.ImageField(upload_to="image/%Y/%m",default=u"image/default.png",max_length=100)

    class Meta:
        verbose_name="用户信息"
        verbose_name_plural=verbose_name

    def __unicode__(self):
        return self.username

如下是修改的代码 ,然后在admin.py中注册了 UserProfile

在django自带后台中添加一条用户数据的时候报错了

1个回答

1

缺少外键:

from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import User                    #导入admin的User

class UserProfile(AbstractUser):
    user = models.OneToOneField(User) # 添加这行

撰写答案