Hi all (again),
I'm hoping someone can help me with the syntax for this django
query ....
I have three related tables
class User(models.Model):
# stuff ...
class Content(models.Model):
# stuff ...
class Relationship(models.Model):
user = models.ForeignKey(User)
content = models.ForeignKey(Content)
hasRead = models.BooleanField()
I want to get all the Content that the User has NOT read.
The catch:
The Relationship record between the User and the Content may not
exist. It only exists if a piece of content is actually read. If they
haven't, the record doesn't exist.
If I try:
content = Content.objects.filter(relationship_user = user,
relationship__hasRead = False)
I don't get any records. So I try to get crafty:
content = Content.objects.filter( Q(relationship__isnull = False) |
Q(relationship__hasRead = False)).filter(relationship_user = user)
has the same problem.
Looking at the raw SQL you can see the problem:
FROM "content"
INNER JOIN "relationship"
ON "content"."id" = "content__relationship"."content_id"
WHERE
"content__relationship"."id" IS NULL
OR
"content__relationship"."hasRead" = False
--and--
FROM "content"
INNER JOIN ...
WHERE
"content__relationship"."content_id" IS NULL
OR
"relationship"."hasRead" = False
well, I'm sure you see the problem. Perhaps what I need is a subquery,
but I'm not sure.
Can I phrase such a query with the django ORM?
Any suggestions?
Thanks
Sandy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---