0

The following is to get 1 record out of database by matching enum type.

TYPES = (
    ('ABC_ABC', 'abc abc'),
    ('XYZ_XYZ', 'xyz xyz'),
)


class Hello(models.Model):    
  type = models.CharField(max_length=8, choices=TYPES, blank=True)

database:

'1', 'ABC_ABC', 'other data'

Queryset:

qset = Q(type__in=('ABC_ABC'))
hello = models.Hello.objects.filter(qset)

Output:

print('count: {}'.format(hello.count()))

Result is 0. It should be 1. What's wrong?

1 Answer 1

1

Try changing your Q filter as follow:

qset = Q(type='ABC_ABC')

Explanations: When using in lookup, django expects an iterable. As you provided only one value, I suspect, it tried to iterate over the string 'ABC_ABC' which is why you didn't get any hit.

If you really want to use the in lookup, which is useless in this case, you should add a comma in to force creating a 1-tuple.

qset = Q(type__in=('ABC_ABC',))

Further thoughts

As this query is quite basic, using a Q object is superfluous. You can simply call

.filter(type='ABC_ABC')
Sign up to request clarification or add additional context in comments.

1 Comment

If it solved your problem, maybe you could mark it as your chosen answer? ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.