|
List Info
Thread: A few questions about queryset's.
|
|
| A few questions about queryset's. |

|
2006-08-31 15:22:54 |
Hi
I've been learning to love django lately, but I can't
quite wrap my
head around more advanced querysets. I've listed my
classes and their
sql tables on the bottom, as all problems are linked to
those tables.
First problem:
I have a class, Content, with a set of tags, how can I
filter for
multiple tags?
If I do:
list = Content.objects.filter(tag__id=1).filter(tag__id=2)
it makes a
sql query asking content_content_tags for content_id's
where tag_id = 1
and tag_id = 2, which will of course never match.
What I'd like to do is something like:
for tag in request.GET.getlist('tag')
content = content.filter('match this tag too')
Second problem:
I've looked at complex_filter for doing this and I can't
quite figure
it out.
What if I wanted something like this:
content.objects.filter(id__id=1) |
content.objects.filter(id__id=2)
But I needed to match a dynamic list of id's. How could I
do something
like
content = content.objects.filter(content=example)
for match in id_list:
content = content.objects.filter(id__id=match) #And here
get a logical
OR comparison with the previus part of the loop.
So this would produce the same effect as:
content.objects.filter(id__id=id_list[0]) |
content.objects.filter(id__id=id_list[1])
Third problem: (promise I'm done soon :p )
And this is something I can't quite wrap my head around. I
want to
allow each individual user to sort by percentage of files
seen.
Sorting content that is.
So I want to produce a list of Content, but sort it based on
the
percentage of FileName objects in that content category the
user has
seen.
I'm thinking I'll need to override the save() function in
FileName, and
update the percentage, stored in a different table. But
since this
percentage has to be stored per user, and I somehow have to
sort
content by it I dont have a foggy clue how to make my
classes.
Does something like this make sense: (where it's updated on
FileName.save())
class SeenCache(models.Model):
content = models.ForeignKey('Content')
user = models.ForeignKey(User)
percentage = models.IntegerField()
It'd only work if a user has seen one file in that category
though.
Classes:
class Tag(models.Model):
name = models.CharField(maxlength=20)
class FileName(models.Model):
fullname = models.CharField(maxlength = 1024,core=True)
content = models.ForeignKey('Content')
#List of users that has seen this content
users_seen =
models.ManyToManyField(User,core=True,null=True,blank=True)
class Content(models.Model):
name = models.CharField(maxlength =
200,null=True,blank=True)
tags = models.ManyToManyField(Tag,null=True,blank=True)
Produced sql tables:
CREATE TABLE "content_content" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(200) NULL,
);
CREATE TABLE "content_content_tags" (
"id" integer NOT NULL PRIMARY KEY,
"content_id" integer NOT NULL REFERENCES
"content_content" ("id"),
"tag_id" integer NOT NULL REFERENCES
"content_tag" ("id"),
UNIQUE ("content_id", "tag_id")
);
CREATE TABLE "content_filename" (
"id" integer NOT NULL PRIMARY KEY,
"fullname" varchar(1024) NOT NULL,
"content_id" integer NOT NULL REFERENCES
"content_content" ("id")
);
CREATE TABLE "content_filename_users_seen" (
"id" integer NOT NULL PRIMARY KEY,
"filename_id" integer NOT NULL REFERENCES
"content_filename"
("id"),
"user_id" integer NOT NULL REFERENCES
"auth_user" ("id"),
UNIQUE ("filename_id",
"user_id")
);
CREATE TABLE "content_tag" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(20) NOT NULL,
);
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| A few questions about queryset's. |

|
2006-08-31 16:58:19 |
On 08/31/06 17:22, Anders Aagaard wrote:
> Hi
>
> I've been learning to love django lately, but I can't
quite wrap my
> head around more advanced querysets. I've listed my
classes and their
> sql tables on the bottom, as all problems are linked to
those tables.
>
> First problem:
> I have a class, Content, with a set of tags, how can I
filter for
> multiple tags?
> If I do:
> list =
Content.objects.filter(tag__id=1).filter(tag__id=2) it makes
a
> sql query asking content_content_tags for content_id's
where tag_id = 1
> and tag_id = 2, which will of course never match.
>
Something like this should work:
mylist = Content.objects.filter(tag_id__range=(1,2,3,4,5))
or:
from django.db.models import Q
mylist = Content.objects.filter(Q(tag_id=1) | Q(tag_id=2))
> What I'd like to do is something like:
> for tag in request.GET.getlist('tag')
> content = content.filter('match this tag too')
>
>
>
> Second problem:
> I've looked at complex_filter for doing this and I
can't quite figure
> it out.
> What if I wanted something like this:
> content.objects.filter(id__id=1) |
content.objects.filter(id__id=2)
> But I needed to match a dynamic list of id's. How
could I do something
> like
>
> content = content.objects.filter(content=example)
> for match in id_list:
> content = content.objects.filter(id__id=match) #And
here get a logical
> OR comparison with the previus part of the loop.
> So this would produce the same effect as:
> content.objects.filter(id__id=id_list[0]) |
> content.objects.filter(id__id=id_list[1])
>
from django.db.models import Q
query = Q(tag_id=1)
for the_tag_id in (2,5,6,8,9):
query = query | Q(tag_id=the_tag_id)
mylist = Content.objects.filter(query)
Be careful with your use of the the double underscore __
It has special meaning in djangoland.
Have a look at
ht
tp://www.djangoproject.com/documentation/db_api/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| A few questions about queryset's. |

|
2006-08-31 16:58:19 |
On 08/31/06 17:22, Anders Aagaard wrote:
> Hi
>
> I've been learning to love django lately, but I can't
quite wrap my
> head around more advanced querysets. I've listed my
classes and their
> sql tables on the bottom, as all problems are linked to
those tables.
>
> First problem:
> I have a class, Content, with a set of tags, how can I
filter for
> multiple tags?
> If I do:
> list =
Content.objects.filter(tag__id=1).filter(tag__id=2) it makes
a
> sql query asking content_content_tags for content_id's
where tag_id = 1
> and tag_id = 2, which will of course never match.
>
Something like this should work:
mylist = Content.objects.filter(tag_id__range=(1,2,3,4,5))
or:
from django.db.models import Q
mylist = Content.objects.filter(Q(tag_id=1) | Q(tag_id=2))
> What I'd like to do is something like:
> for tag in request.GET.getlist('tag')
> content = content.filter('match this tag too')
>
>
>
> Second problem:
> I've looked at complex_filter for doing this and I
can't quite figure
> it out.
> What if I wanted something like this:
> content.objects.filter(id__id=1) |
content.objects.filter(id__id=2)
> But I needed to match a dynamic list of id's. How
could I do something
> like
>
> content = content.objects.filter(content=example)
> for match in id_list:
> content = content.objects.filter(id__id=match) #And
here get a logical
> OR comparison with the previus part of the loop.
> So this would produce the same effect as:
> content.objects.filter(id__id=id_list[0]) |
> content.objects.filter(id__id=id_list[1])
>
from django.db.models import Q
query = Q(tag_id=1)
for the_tag_id in (2,5,6,8,9):
query = query | Q(tag_id=the_tag_id)
mylist = Content.objects.filter(query)
Be careful with your use of the the double underscore __
It has special meaning in djangoland.
Have a look at
ht
tp://www.djangoproject.com/documentation/db_api/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| A few questions about queryset's. |

|
2006-08-31 17:53:05 |
Steven Armstrong wrote:
> On 08/31/06 17:22, Anders Aagaard wrote:
> > Hi
> >
> > I've been learning to love django lately, but I
can't quite wrap my
> > head around more advanced querysets. I've listed
my classes and their
> > sql tables on the bottom, as all problems are
linked to those tables.
> >
> > First problem:
> > I have a class, Content, with a set of tags, how
can I filter for
> > multiple tags?
> > If I do:
> > list =
Content.objects.filter(tag__id=1).filter(tag__id=2) it makes
a
> > sql query asking content_content_tags for
content_id's where tag_id = 1
> > and tag_id = 2, which will of course never match.
> >
>
> Something like this should work:
>
> mylist =
Content.objects.filter(tag_id__range=(1,2,3,4,5))
>
> or:
>
> from django.db.models import Q
> mylist = Content.objects.filter(Q(tag_id=1) |
Q(tag_id=2))
Your misunderstanding me, I dont want to browse objects with
tag A OR
B, I want tag A AND B in this example.
What I thought would work:
#a = Content.objects
#b = a.filter(tags__id = 8)
#print b[0].tags.values()
[{'id': 3, 'name': 'TestTag'}]
#b.filter(tags__id = 3)
[]
Why doesn't the b.filter return b[0]? Which does have that
tag?
>
>
> > What I'd like to do is something like:
> > for tag in request.GET.getlist('tag')
> > content = content.filter('match this tag too')
> >
> >
> >
> > Second problem:
> > I've looked at complex_filter for doing this and
I can't quite figure
> > it out.
> > What if I wanted something like this:
> > content.objects.filter(id__id=1) |
content.objects.filter(id__id=2)
> > But I needed to match a dynamic list of id's.
How could I do something
> > like
> >
> > content = content.objects.filter(content=example)
> > for match in id_list:
> > content = content.objects.filter(id__id=match)
#And here get a logical
> > OR comparison with the previus part of the loop.
> > So this would produce the same effect as:
> > content.objects.filter(id__id=id_list[0]) |
> > content.objects.filter(id__id=id_list[1])
> >
>
> from django.db.models import Q
> query = Q(tag_id=1)
> for the_tag_id in (2,5,6,8,9):
> query = query | Q(tag_id=the_tag_id)
> mylist = Content.objects.filter(query)
Ahh! So logical and clean, thanks a ton!
>
> Be careful with your use of the the double underscore
__
> It has special meaning in djangoland.
Yeah, of course it'd be id=id_list[0], things tend to get
tangled when
I try to sort this in my head ;)
>
> Have a look at
> ht
tp://www.djangoproject.com/documentation/db_api/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
[1-4]
|
|