python - Checking if any object in a queryset is in a ManyToMany relation -


what i'd able similar pseudo-code - i'm unaware of how in python:

    user_groups = request.user.participant_groups.all()     if group in user_groups not in self.object.settings.groups.all():  

basically, i'd check if of objects in user_groups in self.object.settings.groups.all(). there simple way this?

models:

class group(models.model):     participants = models.manytomanyfield('auth.user', null=true, blank=true, related_name='participant_groups')     title = models.charfield(max_length=180)     date = models.datetimefield(null=true, blank=true, editable=false)     modified = models.datetimefield(null=true, blank=true, editable=false)  class settings(models.model):     user = models.foreignkey('auth.user', related_name='settings_objects')     groups = models.manytomanyfield('groups.group', null=true, blank=true)     participants = models.manytomanyfield('auth.user', null=true, blank=true, related_name='accessible_objects')     private = models.booleanfield(default=true) 

what i'm trying check if of user's participant_groups (reverse relation user on group model) in settings objects groups manytomany relation.

try -

common_groups = user.participant_groups.annotate(     num_settings=count('settings_objects') ).filter(num_settings__gt=0)  # can count count_of_above = common_groups.count() 

i'm assuming self.object.settings instance of settings current user. should make clear.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -