Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: the value of list_display[4] refers to 'age', which is not a callable, etc

Tags:

python

django

I'm trying to extend the User model to add additional attributes to my "Person" model when I create instances of it, and I keep getting the following errors:

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'.

Person model:

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 view:

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')

Snippets of 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, )

Any ideas on how to get this working?

like image 253
TyCharm Avatar asked Jan 23 '16 21:01

TyCharm


People also ask

How do I change the administrator name in Django?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.

What is the purpose of the admin site in a Django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


1 Answers

You can't use admin.site.register(User, PersonAdmin), since User and Person are not the same model. Also, it looks like you are trying to include these Person model fields inside the User admin:

from django.contrib.auth.admin import UserAdmin

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

    fields = ('username', 'email', 'first_name', 'last_name', 'age', 'city', 'state')

class UserAdmin(UserAdmin):
    inlines = [
        PersonInline
    ]

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
like image 96
Hybrid Avatar answered Dec 07 '22 07:12

Hybrid