Home > Enterprise >  how to filter objects by a specific choice
how to filter objects by a specific choice

Time:01-27

I have model like this:

class ScientificInfo(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    is_approved = models.CharField(max_length=64, choices=(('0', 'yes'), ('1', 'no')), blank=True)
    is_interviewed = models.BooleanField(default=False)

how can I filter this model by is_approved field which is a choicefield? I wrote this line but doesnt work

approved = ScientificInfo.objects.filter(is_approved__in='0').all()

CodePudding user response:

Using the exact field lookup would probably make more sense here:

approved = ScientificInfo.objects.filter(is_approved__exact='0').all()

https://docs.djangoproject.com/en/4.0/ref/models/querysets/#exact

CodePudding user response:

Put values inside () as

approved = ScientificInfo.objects.filter(is_approved__in=('0')).all()
  •  Tags:  
  • Related