0

I have 2 models

class Service(models.Model):
    name = models.CharField(max_length=100)

class ServiceType(models.Model):
    name = models.CharField(max_length=100)
    services = models.ManyToManyField(Service, related_name='service_types')

Now i have a list of services and want to get ServiceType with only this services. If not exists, create it.

I don't understand how to build this queryset threw Django ORM.....

queryset like

ServiceType.objects.filter(services__id__in=services.values_list('id', flat=True)).annotate(count=Count('services')).filter(count=services.count())

returns all ServiceTypes with needed services and not needed services...

P.S. Sorry for my English

Tapo4ek
  • 313
  • 2
  • 7
  • I'm having trouble understanding your question. For a set of specific services you want to find the service type that offers exactly these services? Can you maybe add a simple data example that shows what you are looking for? Assume you have 3 services A, B and C. Then assume you have service type 1 - offering A and B - and service type 2 - offering B and C. Do you want to search for a service type that only offers A and B? – Philip B. Feb 21 '18 at 04:36
  • For example i have services, A, B, C and D. And i have 2 service_types ServiceType(AB), that provides services A and B, ServiceType(CD) that provides services C and D. For set of services A and B, queryset must return ServiceType(AB), for set of services B and C, there will be empty queryset, for set of services C and D, ServiceType(CD) will be returned. – Tapo4ek Feb 24 '18 at 16:48

0 Answers0