Frost
Frost

Reputation: 3896

django contains any query

I am trying to make a query that selects all uuids of ProblemSet whose problems contains at least one problem with a specific problem type. How can I do it in Django? In mysql it will be a simple join but django's '__contains' doesn't serve the needs..

Thanks!!

class ProblemType:
  name ..... (many fields)

class Problem:
  problem_type = models.ManyToManyField(ProblemType)
  ...... (many fields)

class ProblemSet:
  problems = models.ManyToMnayField(Problem)
  uuid = models.CharField(...)
  ...... (many fields)

Upvotes: 0

Views: 86

Answers (1)

voithos
voithos

Reputation: 70562

Does a normal filter not work?

uuids = ProblemSet.objects.filter(problems__problem_type__name='MyProblemType')
    .values_list('uuid', flat=True)

Also, do your problems have multiple problem_types? If so, then you should reflect that fact by renaming your ManyToManyField as problem_types (note the pluralization). Otherwise, you shouldn't use a ManyToManyField.

Upvotes: 2

Related Questions