Django Admin - ' UserProfile'对象没有属性' first_name'

时间:2016-03-26 12:08:05

标签: python django django-models django-forms django-admin

我尝试UserProfile使用OneToOneField在Django中扩展User似乎有效。然后,我创建了一个注册视图和表单,以便能够使用User模块注册新的UserProfile

问题在于,当我使用此表单创建User时,当我点击Users页面中的Admin按钮时,Django管理员开始出现错误。

编辑:可能值得一提的是,当我创建了一个新用户时,ID并不是唯一的例外,这可能是由模型中的post_save方法引起的。< / p>

你知道问题出在哪里或我该怎么做?

enter image description here 以下是我的代码:

VIEWS.PY

def register(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        profile_form = UserProfileForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()
            return register_success(request)
        else:
            print user_form.errors, profile_form.errors

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render(request,"auth/registration/register.html",context={'user_form':user_form,'profile_form':profile_form}) 

FORMS.PY

class ContactUsForm(forms.Form):
    contact_name = forms.CharField(required=True)
    from_email = forms.EmailField(required=True)
    subject = forms.CharField(required=True)
    message = forms.CharField(
            required=True,
            widget=forms.Textarea
    )


class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model = User
        fields = ('username', 'email', 'password')

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('telephone',)

MODELS.PY

class UserProfile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)

    # ATRIBUTY KTORE BUDE MAT KAZDY
    telephone = models.CharField(max_length=40)

    HOW_DO_YOU_KNOW_ABOUT_US_CHOICES = (
            ('coincidence',u'It was coincidence'),
            ('relative_or_friends','From my relatives or friends'),
            )
    how_do_you_know_about_us = models.CharField(max_length=40, choices=HOW_DO_YOU_KNOW_ABOUT_US_CHOICES, null=True)

    MARITAL_STATUS_CHOICES = (
        ('single','Single'),
        ('married','Married'),
        ('separated','Separated'),
        ('divorced','Divorced'),
        ('widowed','Widowed'),
    )
    marital_status = models.CharField(max_length=40, choices=MARITAL_STATUS_CHOICES, null=True)

    # OD KIAL STE SA O NAS DOZVEDELI
    # A STAV

    def __unicode__(self):
        return '{} {}'.format(self.first_name,self.surname)

def create_profile_user_callback(sender,instance, **kwargs):
    profile, new = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_profile_user_callback, User)

ADMIN.PY

admin.site.register(AdminContact)

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'User_Profile'

class UserAdmin(BaseUserAdmin):
    inlines = (UserProfileInline, )

admin.site.unregister(User)
admin.site.register(User,UserAdmin)

2 个答案:

答案 0 :(得分:3)

__unicode__方法,在 UserProfile 模型中,它使用self.first_nameself.sur_name,这在此模型中实际上不存在。

self.user.first_name是合适的。

答案 1 :(得分:0)

问题出在您的代码中..

def unicode (个体经营):     return'{} {}'.format(self.first_name,self.surname)

在这里,您拨打self.first_name,self.surname,但未在UserProfile

中定义

定义first_namesurname

喜欢

first_name= models.CharField(max_length=255)
surname= models.CharField(max_length=255)

在您的UserProfile班级