Django-Filter apps,How to filter foreign key?

class IssueFilter(django_filters.FilterSet):
    STATUS = (
        ('',''),
        ('Pending', 'Pending'),
        ('Granted','Granted'),
        ('Denied','Denied'),
        )
    status = django_filters.ChoiceFilter(choices=STATUS)
    name = django_filters.CharFilter(field_name='item',lookup_expr='exact')
    quantity = django_filters.NumberFilter(field_name='quantity', lookup_expr='gt')
    class meta:
        model: Issue

I was able to gilter status ad quantity, however since item field is a foreign key and is an int, i can’t exacly use charfield

The django-filter docs cover this.

Hello,sorry i reas it,but when i tried as
field: '__all__'
is deprecated,
i need to declare it manually,as most of the tutorial i have seen are autogenerate them by that form

I’m sorry, I might not be understanding the issue here.

Which line of code are you having problems with?

I thought you were talking about this one:
name = django_filters.CharFilter(field_name='item',lookup_expr='exact')
and that you were asking how to set this filter to compare with the name attribute of the item object. So I pointed you to the docs to reference the field_name parameter to see how to access a field through a ForeignKey relationship.

1 Like

You’re close
name = django_filters.CharFilter(field_name='item',lookup_expr='exact')
the thing is my model is

class Item(models.Model):
    CATEGORY = (
        ('Gudang Kering', 'Gudang Kering'),
        ('Gudang Basah','Gudang Basah'),
        )
    name = models.CharField(max_length=200,null= True)
    stock = models.IntegerField(default='0', blank=False, null=True)
    category = models.CharField(max_length=200,null= True,choices=CATEGORY)
    reorderlevel = models.IntegerField(default='0', blank=False, null=True)
    maxreorderlevel = models.IntegerField(default='0', blank=False, null=True)
    description = models.CharField(max_length=200,null= True, blank= True)
    date_created = models.DateTimeField(auto_now_add= True)
    tags = models.ManyToManyField(Tag)
    
    def __str__(self):
        return self.name

class Issue(models.Model):
    STATUS = (
        ('Pending', 'Pending'),
        ('Granted','Granted'),
        ('Denied','Denied'),
        )
    customer = models.ForeignKey(Customer, null=True, on_delete= models.SET_NULL)
    item = models.ForeignKey(Item, null=True, on_delete= models.SET_NULL)
    quantity = models.IntegerField(default='0', blank=False, null=True)
    date_created = models.DateTimeField(auto_now_add=True, auto_now=False)
    status = models.CharField(max_length=200,null= True, choices=STATUS)

    def __str__(self):
        return str(self.quantity)+' '+str(self.item)+' '+self.status + '  ' +str(self.customer)

it returned the value of item(foreignkey) as an int,so i can’t use charfilter.
i tried booleanfield, it returned the name but unlike version from before where we can type the item name to search it became a choice like field where i chose inputed data from before on search
in the past i just need to do my form like so

class IssueFilter(django_filters.FilterSet):

    class meta:
        model = Issue
        fields = '__all__'

#when called this
        fields = '__all__'

will make it so django filter autogenerate the search form to include all fields from model

Ok, I’m still confused here. Are we talking about one issue or two?

Lets try to focus on one thing at a time.

This line and only this line:
name = django_filters.CharFilter(field_name='item',lookup_expr='exact')

Is the issue that you want to filter by the name attribute of the Item object?

If so, then the answer is what I referenced above. (Note: The forum software shows when someone has clicked on an embedded link. I do not see where you’ve gone to that referenced site.)

If this is not your question, then I need to figure out what it is that you’re trying to accomplish.