0

What I'd like to be able to do is similar to this pseudo-code - I'm just completely unaware of how to do this in python:

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

Basically, I'd like to check if any of the objects in user_groups are in self.object.settings.groups.all(). Is there a simple way to do 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 to do is check if any of a user's participant_groups (reverse relation to user on group model) are in a settings objects groups manytomany relation.

  • Did you read the Django documentation for manytomany relationship? – Bibhas Debnath Mar 24 '14 at 18:23
  • @Bibhas Yes I have. Is there anything you think might help me specifically? –  Mar 24 '14 at 18:29
  • Don't think you have. It's explained in the ManyToMany relationship documentation with example how to do `IN` queries with it. And what is `self.object.settings.groups.all()`? A queryset? A dictionary? A list? – Bibhas Debnath Mar 24 '14 at 18:33
  • It's a reverse ManyToManyField, so yes, a queryset. None of the examples there are similar to my use case though. Both of my querysets are coming from related fields on already fetched models. –  Mar 24 '14 at 18:38
  • Can you please show the models? Or people will have to guess what you're asking. Explain your exact problem. – Bibhas Debnath Mar 24 '14 at 18:40

1 Answers1

0

Try this -

common_groups = user.participant_groups.annotate(
    num_settings=Count('settings_objects')
).filter(num_settings__gt=0)

# You can get a count like this
count_of_above = common_groups.count()

I'm assuming self.object.settings is an instance of Settings for the current user. You should make it clear.

Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
  • `request.user.participant_groups.all()` isn't this already an iterable. settings_with_groups_in_users_participant_groups = Settings.objects.filter( user=request.user, groups__in=request.user.participant_groups.all() ) – Kracekumar Mar 24 '14 at 19:16
  • @kracekumar Yes it is. Just wasn't sure if `__in` takes querysets. Just confirmed. Thanks – Bibhas Debnath Mar 24 '14 at 19:18
  • @kracekumar But not preferable according to the doc. Better to extract a list of ids and pass that https://docs.djangoproject.com/en/1.7/ref/models/querysets/#in – Bibhas Debnath Mar 24 '14 at 19:22
  • I must have not explained myself very well. The queryset I'm hoping to end up with is of Group objects - it's that which I need a count of. I'll try again. A settings object can have multiple groups. A user can also have multiple groups. I'm trying to see if any of a users groups are in a settings object's groups. So I'm not trying to collect a Settings queryset. –  Mar 24 '14 at 19:32
  • In *a settings object's groups*? Or In the groups of the settings object belonging to that user? – Bibhas Debnath Mar 24 '14 at 19:34
  • In the ManyToManyField named groups on a particular settings object. So, yeah. –  Mar 24 '14 at 19:39