0

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

class FirstName(models.Model):
    name = models.CharField(max_length=50, unique=True)


class Fighter(models.Model):
    first_name = models.ForeignKey(FirstName, on_delete=models.CASCADE)


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.

Kyle
  • 11
  • 2
  • can you elaborate a bit more please? what result are you trying to achieve? – Druhin Bala Sep 01 '22 at 00:49
  • @Bruhin Bala Long and short of it is I want to reference the name field from the Fighter table. It is linked to the FighterStats table(one to many) which is in turn linked to the Fight table(one to many) which is finally linked to the Event table(one to many). So the final stack would be Event->Fight(fights)->FighterStats(fighters)->Fighter(fighter)->first_name(foreign_key)->name(field). The main issue is I don't know how to reference sets within sets(fighters within fights) – Kyle Sep 01 '22 at 01:27
  • can you please post your Fighter table? – Druhin Bala Sep 01 '22 at 01:28
  • @Bruhin Bala Added the relevant part to the main post. – Kyle Sep 01 '22 at 01:32

1 Answers1

1

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.

Kyle
  • 11
  • 2