Django Rest Framework - SearchFilter

I need to try get a SearchFilter working for fields within a set within another set.

class Fight(models.Model):
    event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name="fights")


class FighterStats(models.Model):
    fight = models.ForeignKey(Fight, on_delete=models.CASCADE, related_name="fighters")
    fighter = models.ForeignKey(Fighter, on_delete=models.CASCADE, null=True)


class EventSearchFilter(filters.SearchFilter):
    def get_search_fields(self, view, request):
        field_names = {
            "name": "name",
            "venue": "location__venue",
            "fighters": "fights__fighters__fighter__first_name__name"
        }

Don't have issues with getting foreign keys to work but i could use some help with foreign sets. I removed the unrelated fields/code from above.

Turns out I was referencing the fields correctly the issue was that all my fighter fields were null, caused by this line.

fighter_obj = Fighter.objects.filter(fighter_url=fighter_url).first()

Fixed it by changing the fighter_url to lower case before filtering. Lesson learned filter is case sensitive.

Back to Top