python - Filter by presence in a model's set -


i have customer model has many locations, i.e., there location_set attribute on model returns list of locations. each location has many customers, i.e., customer_set attribute.

i have 1 customer instance of corresponding attributes. want return other customers present in @ least of locations in customer's location_set. there clean way without having manually manipulate queryset , make ton of calls db?

class customer(abstractuser):      current_location = models.foreignkey('device.location',             null=true, blank=true, related_name='customers_present')     default_location = models.foreignkey('device.location',             null=true, blank=true, related_name='default_customers')  class location(models.model):      name = models.charfield(max_length=50, help_text="the name of location")     customers = models.manytomanyfield(settings.auth_user_model,             through='customer.membership')  class membership(models.model):     customer = models.foreignkey(customer)     location = models.foreignkey('device.location')     date_joined = models.datetimefield(auto_now_add=true) 

without model definitions difficult provide exact answer question, below work:

customer.objects.filter(location__in=your_customer_instance.location_set.all()).exclude(pk=your_customer_instance.pk)


Comments

Post a Comment

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Socket.connect doesn't throw exception in Android -

SPSS keyboard combination alters encoding -