Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Related only Field list filter- Not RelationField

I want django to only show related fields in the filters when an item is filtered.

For example, if I choose the brand "XYZ", it will only show options in the filter that contain "XYZ" brand.

My code below is taken from another question here- but it doesn't work. I keep getting a "NotRelationField" error (http://dpaste.com/23Y8ZE3).

Admin.py---

from django.contrib.admin.filters import RelatedOnlyFieldListFilter

# Some SimpleListFilter filters

class ProductAdmin(admin.ModelAdmin):
    actions = ['tag_Active_Wear', 'tag_Trending',]
    list_filter = (
        ('brand', RelatedOnlyFieldListFilter),
    )
    admin_order_field = ('price',)

# Some querysets for the actions

admin.site.register(Product, ProductAdmin)

Models.py----

class Product(models.Model):
    name = models.CharField ("Name", max_length=400)
    store = models.ForeignKey(Store)
    brand = models.CharField("Brand", max_length=200, blank=True)
    category = models.ManyToManyField(Category, blank=True)
    def __unicode__(self):
       return self.name
like image 684
Ycon Avatar asked Nov 22 '15 09:11

Ycon


1 Answers

it should be store instead of brand, because brand is not a RelatedField

list_filter = (
    ('store', RelatedOnlyFieldListFilter),
)
like image 181
doniyor Avatar answered Sep 25 '22 15:09

doniyor