Django:list_display [4]的值是指'age',它不是可调用的等等

时间:2016-01-23 21:49:40

标签: python django

当我创建它的实例时,我正在尝试扩展User模型以向我的“Person”模型添加其他属性,并且我不断收到以下错误:

ERRORS:
<class 'polls.admin.PersonAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'age', which is not a callable, an attribute of 'PersonAdmin', or an attribute or method on 'auth.User'.
<class 'polls.admin.PersonAdmin'>:(admin.E108) The value of 'list_display[5]' refers to 'city', which is not a callable, an attribute of 'PersonAdmin', or an attribute or method on 'auth.User'.
<class 'polls.admin.PersonAdmin'>: (admin.E108) The value of 'list_display[6]' refers to 'state', which is not a callable, an attribute of 'PersonAdmin', or an attribute or method on 'auth.User'.

人物模型:

class Person(models.Model):
    """ The model which defines a user of the application. Contains important
    information like name, email, city/state, age, etc """
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    city = models.CharField(max_length=200, null=True)
    state = models.CharField(max_length=200, null=True)
    age = models.CharField(max_length=50, null=True)

Create_account视图:

def create_account(request):
    # function to allow a user to create their own account
    if request.method == 'POST':
        # sets a new user and gives them a username
        new_user = User(username = request.POST["username"],
                    email=request.POST["email"],
                    first_name=request.POST["first_name"],
                    last_name=request.POST["last_name"],
                    age=request.POST["age"],
                    city=request.POST["city"],
                    state=request.POST["state"]
                    )

        # sets an encrypted password
        new_user.set_password(request.POST["password"])
        new_user.save()
        # adds the new user to the database
        Person.objects.create(user=new_user,
                          first_name=str(request.POST.get("first_name")),
                          last_name=str(request.POST.get("last_name")),
                          email=str(request.POST.get("email")),
                          age=str(request.POST.get("age")),
                          city=str(request.POST.get("city")),
                          state=str(request.POST.get("state"))
                          )
        new_user.is_active = True
        new_user.save()
        return redirect('../')
    else:
        return render(request, 'polls/create_account.html')

admin.py小部件:

class PersonInline(admin.StackedInline):
    """ Details a person in line. """
    model = Person
    can_delete = False
    verbose_name_plural = 'person'

class PersonAdmin(admin.ModelAdmin):
    """ Defines a Person's information on the admin site
    with displays listed below. """
    list_display = ('username', 'email', 'first_name', 'last_name', 'age', 'city', 'state')
    inlines = (PersonInline, )

有关如何使这项工作的任何想法?

3 个答案:

答案 0 :(得分:5)

您无法使用 $('#myValue input').focus(); ,因为admin.site.register(User, PersonAdmin)User不是同一型号。此外,您似乎试图在用户管理员中包含这些人员模型字段:

Person

答案 1 :(得分:0)

就我而言,我必须从应用程序的admin.py文件中删除我在模型中定义的ManyToMany字段,并在项目的urls.py中注释掉该应用程序的include()。

答案 2 :(得分:0)

<class 'polls.admin.PersonAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'age', which is not a callable, an attribute of 'PersonAdmin', or an attribute or method on 'auth.User'.

以上几行说明的是,

  • age 必须是 PersonAdmin 类的属性
  • 或在 auth.User 文件中声明的属性或方法

还要检查代码中的一些拼写错误。我在 models.py 中命名 slug,在 admin.py 中命名为 Slug,它产生了同样的错误。