Finding details of a Model object, which do not have objects of a given M2M field

4 views
Skip to first unread message

shabda

unread,
Jan 26, 2008, 7:14:06 AM1/26/08
to Django users
I have the model like,

class Blog(models.Model):
url = models.URLField()

class Entry(models.Model):
blog = models.ForeignKey(Blog)
user = models.ManyToManyField(User)

User is contrib.auth.db.USER

Now I given a blog and a user I need to find out all the Entry objects
which are a detail of this blog and do not have the given user. How
can I do that?

alex....@gmail.com

unread,
Jan 26, 2008, 10:54:59 AM1/26/08
to Django users
Blog.entry_set.filter(user__ne=some_uesr)

shabda

unread,
Jan 26, 2008, 12:41:44 PM1/26/08
to Django users
Should for a M2M field
ModelClass.objects.all() be same as ModelClass.objects.filter(some_m2m
= some_m2m) UNION ModelClass.objects.exclude(some_m2m = some_m2m)? If
yes then probably there is a bug. To test define a model class as in
[1]. Do the tests like in [2]. You .all() does not give same results
as .filter() + .exclude() .

BTW, there is no __ne field lookup.[3]
[1]http://dpaste.com/32649/
[2]http://dpaste.com/32648/
[3]http://www.djangoproject.com/documentation/db-api/

On Jan 26, 8:54 pm, "alex.gay...@gmail.com" <alex.gay...@gmail.com>
wrote:

alex....@gmail.com

unread,
Jan 26, 2008, 6:23:36 PM1/26/08
to Django users
Doh, this is what happens when I post before my second dose of
caffeine, there are no negative filters there is just the exclude
method, so the original thing should be:

blog = Blog.objects.get(pk=something)
entries = blog.entry_set.exclude(user=some_user)
Reply all
Reply to author
Forward
0 new messages