Home > Software design >  how can i use django filter with multiple value select
how can i use django filter with multiple value select

Time:12-15

I want to filter my data based on city , how can i filter my data if the user choose more than one city using django filter

class games(generics.ListAPIView):
queryset = Game.objects.filter(start_date__gte=datetime.today())
serializer_class=GameSerializers
filter_backends = [DjangoFilterBackend,filters.OrderingFilter]
filterset_fields = ['id','city','level']

game model

class Game(models.Model):
city = models.CharField(max_length=255)
gender = models.ForeignKey(Gender,on_delete=models.CASCADE)
level = models.ForeignKey(Level,on_delete=models.CASCADE)
host = models.ForeignKey(Host,on_delete=models.CASCADE)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
fees = models.IntegerField()
indoor = models.BooleanField()
capacity = models.IntegerField()
age_from = models.IntegerField()
age_to = models.IntegerField()
description =  models.CharField(max_length=255)
earned_points = models.IntegerField()
created_at = models.DateTimeField(default=django.utils.timezone.now)
image = models.ImageField(upload_to="GameImage",null=True)
history = HistoricalRecords()

CodePudding user response:

Game.objects.filter(city__in=['Paris', 'London'])

Something like that ?

CodePudding user response:

I'm not sure if this gonna work but try this:

filterset_fields = ['id','city__in','level']
  • Related