views:

565

answers:

2
class Domains(models.Model):
    name = models.CharField(max_length=30)
    description = models.CharField(max_length= 60)
    user = models.ManyToManyField("Users", blank=True, null=True)
    def __unicode__(self):
        return self.name

class Groups(models.Model):
    domain = models.ForeignKey(Domains)
    name = models.CharField(max_length=30)
    description = models.CharField(max_length= 60)
    def __unicode__(self):
        return self.name

class Users(models.Model):
    login = models.CharField(max_length=30, unique=True)
    group = models.ManyToManyField(Groups, blank=True, null=True)
    def __unicode__(self):
        return self.login

I have the model above. Needed some assistance working with Django ORM. How would I build a query the returns all the Group names that belong to only those Domains to which a User belongs

+2  A: 

You should probably use singular names for your model classes. For example, I'd rewrite the models as:

class Domain(models.Model):
    name = models.CharField(max_length=30)
    description = models.CharField(max_length= 60)
    user = models.ManyToManyField('User', blank=True, null=True)

    def __unicode__(self):
            return self.name

class Group(models.Model):
    domain = models.ForeignKey(Domain, related_name='groups')
    name = models.CharField(max_length=30)
    description = models.CharField(max_length= 60)

    def __unicode__(self):
            return self.name

class User(models.Model):
    login = models.CharField(max_length=30, unique=True)
    group = models.ManyToManyField(Group, related_name='users', blank=True, null=True)

    def __unicode__(self):
            return self.login

Since you have users directly related to groups, you don't need to involve domains at all. To fetch all group names for a particular user, you'd do:

Group.objects.filter(users__pk=...).values_list('name', flat=True)

Replace '...' with the ID of the user you're interested in.

elo80ka
I actually need the Domain because I want to show even those groups that are not associated with a User but are associated with a Domain to which the User belongs
Ah...I missed that. Jarret's solution is probably what you need then.
elo80ka
+4  A: 

I second elo80ka's comment about using singular names for your models. To filter the groups by domain and user, try:

Groups.objects.filter(domain__user=u)

This will perform the appropriate join across the many-to-many. As written, the query will return group objects. If you want the name property only, then append the .values_list('name', flat=True) to the query as elo80ka suggests.

Jarret Hardie
class MyUserAdminForm(forms.ModelForm): class Meta: model = Users group = forms.ModelMultipleChoiceField( queryset=Groups.objects.filter(???), widget=forms.CheckboxSelectMultiple, )Sorry Jarret..not understanding how this works... what exactly does the filter 'domain__user' do?
The filter joins Groups to the Users table via Domains, so you'll only get groups that whose 'domain' ForeignKey points to a Domain that has the user 'u' in the Domain.users many-to-many field.
Jarret Hardie
Perhaps this section in the django docs will make it clearer: Have a peek at this in the docs: http://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships
Jarret Hardie
Just trying to figure out what would be on the right side of the = sign.class MyUserAdminForm(forms.ModelForm):class Meta:model = Usersgroup = forms.ModelMultipleChoiceField(queryset=Groups.objects.filter(domain__user=???),widget=forms.CheckboxSelectMultiple,)
I am trying to filter the groups in the list that I see in the ModelForm for each User