bsaoptima
bsaoptima

Reputation: 175

The value of 'list_filter[0]' refers to 'people__type', which does not refer to a Field

First time asking a question here so sorry if I'm not doing this perfectly.

Context : Project in django, trying to have a filtering option in my admin panel.

I have this Charfield in my model.py file that I am trying to use as a filter in admin.py, however python tells me that it doesn't refer to a field which is very confusing. Here are my codes :

models.py

class People(models.Model):
    OWNER = 'owner'
    TENANT = 'tenant'
    TYPES = ((OWNER, 'Owner'), (TENANT, 'Tenant'))
    type = models.CharField(max_length=20, choices=TYPES)
    people_name = models.CharField(max_length=200)
    people_surname = models.CharField(max_length=200)
    people_phone_number = models.CharField(max_length=200)
    people_email = models.EmailField(max_length=254)
    people_occupation = models.CharField(max_length=200)
    people_revenue = models.IntegerField()
    people_slug = models.CharField(max_length=200, default=1)

    def __str__(self):
         return self.people_name

admin.py

from django.contrib import admin from django.db import models from .models import * from tinymce.widgets import TinyMCE

#Register your models here.

class PeopleAdmin(admin.ModelAdmin):

    fieldsets = [
        ("Type", {"fields": ["type"]}),
        ("Information", {"fields": ["people_name",
                                    "people_surname",
                                    "people_phone_number",
                                    "people_email",
                                    "people_occupation",
                                    "people_revenue"]}),
        ("URL", {"fields": ["people_slug"]}),
    ]

list_filter = ('people__type',)

admin.site.register(People, PeopleAdmin)

Upvotes: 0

Views: 1089

Answers (2)

JP1
JP1

Reputation: 94

is it a typo? try list_filter = ('types',) since you extended it to the Choices var Types

Upvotes: 0

Lewis
Lewis

Reputation: 2798

Replace:

list_filter = ('people__type',)

With

list_filter = ('type',)

people__type would be used if you trying to filter from a foriegn key relationship to the People model on the type field. Hence the __.

Upvotes: 0

Related Questions