nlassaux
nlassaux

Reputation: 2406

A query with ManyToMany fields

I need to have a view with users that havn't got any group. I have tried to make a query with .except but it has failed because of manytomany fields...

My models :

class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True)
    courses_list = models.ManyToManyField('Course', blank=True)
    group_list = models.ManyToManyField('Group', blank=True)

class Group(models.Model):
    name = models.CharField(max_length=30)
    assignment = models.ForeignKey(Assignment)
    members = models.ManyToManyField(UserProfile, through=UserProfile.group_list.through, blank=True)

class Assignment (models.Model):
    course = models.ForeignKey(Course)

class Course(models.Model):
    subscribed = models.ManyToManyField(UserProfile, through= UserProfile.courses_list.through, blank=True)

So, I need to select users subscribed to the assignment (assignment.course.subscribed.all send all) that haven't got any group (the problem is that I need to take care of groups linked to this assignment only)

I have tried groupless = detailedassignment.course.subscribed.exclude(username = group_list.filter(assignment=detailedassignment).members.username.all()) but it doesn't work at all (and i think it is normal when I see the code..)

Edit :

I have removed relations in a way, now my models are :

My models :

class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True)

class Group(models.Model):
    name = models.CharField(max_length=30)
    assignment = models.ForeignKey(Assignment)
    members = models.ManyToManyField(UserProfile, blank=True)

class Assignment (models.Model):
    course = models.ForeignKey(Course)

class Course(models.Model):
    subscribed = models.ManyToManyField(UserProfile, blank=True)

I have tried to use :

groupless = detailedassignment.course.subscribed.exclude(user__username__in = Group.objects.filter(assignment=detailedassignment).members.objects.values_list('user__username'))

But I have 'QuerySet' object has no attribute 'members'

Upvotes: 0

Views: 167

Answers (2)

nlassaux
nlassaux

Reputation: 2406

So, when the backward reference have been removed, I have used a related_name in my ManyToManyField and Rahul's answer with a little edit :

groupless = User.objects.filter(course_list__assignment=detailedassignment).exclude(group_list__assignment=detailedassignment)

If it can be useful.. :D

Upvotes: 0

Rahul
Rahul

Reputation: 1505

You're forgetting the user reference. So if I interpret you properly, you want something like -

groupless = detailedassignment.course.subscribed.exclude(user__username__in = group_list.filter(assignment=detailedassignment).members.objects.values_list('user__username'))

OR try this -

groupless = UserProfile.objects.filter(courses_list__assignment = detailedassignment).exclude(group_list__assignment=detailedassignment)

Also as an aside, your model design looks somewhat skewed, with same references running across each other. And there's no need to add backward references (like in model `Course). Django gives you the functionality for backward referencing. See https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

Upvotes: 1

Related Questions