如何修复str对象没有属性'_meta'

时间:2019-07-01 10:40:36

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

我正在Django项目中迁移我的模型,但是正在生成错误。下面是代码。为了解决这个问题,我的代码中应该做些什么纠正

Models.py
from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class UserProfileInfo(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)

    #addtional
    portfolio_site = models.URLField(blank=True)

    profile_pic = models.ImageField(blank=True,upload_to="profile_pics")

    def __str__(self):
        return (self.user.username)
Forms.py
from django import forms
from django.contrib.auth.models import User
from basic_app.models import UserProfileInfo

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

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


class UserProfileInfo(forms.ModelForm):
    class Meta():
        model = UserProfileInfo
        fields=['portfolio_site','profile_pic']     
Admin.py
from django.contrib import admin
from basic_app.models import UserProfileInfo


# Register your models here.
admin.site.register('UserProfileInfo')

我越来越  属性错误:“ str”对象没有属性“ _meta”

2 个答案:

答案 0 :(得分:2)

admin.site.register(..) [Django-doc]的参数是一个模型,或者是可迭代的模型;以及可选的模型管理员。不是该模型的字符串。

因此,您应该传递对UserProfileInfo的引用,而不是包含该名称的 string 的。

赞:

# app/admin.py

from django.contrib import admin
from basic_app.models import UserProfileInfo

# an identifier, not a string
admin.site.register(UserProfileInfo)

答案 1 :(得分:2)

无需用字符串包装模型,只需在admin.py中键入模型名称

Admin.py
from django.contrib import admin
from basic_app.models import UserProfileInfo


# Register your models here.
admin.site.register(UserProfileInfo)