Re: Questions about ContentType generic relations

2009-11-16 Thread Daniel Roseman
On Nov 16, 8:12 am, Continuation  wrote:
> > As the docs show, when you've defined a GenericRelation, querying is
> > exactly the same as with a reverse foreign key. So:
> > Bookmark.objects.get(tags__tag='django')
>
> A somewhat related question:
>
> What if instead of having a ForeignKey to ContentType, TaggedItem has
> a direct ForeignKey to Bookmark.
>
> In that case is there something similar to GenericRelation that I can
> define for Bookmark so that I can still use Bookmark.objects.get
> (tags__tag='django') (or something similar) to find all bookmarks with
> the tag "django"?
>
> The doc said "You cannot access a reverse ForeignKey Manager from the
> class; it must be accessed from an instance", but  in this case I
> really need to start with the class Bookmark instead of an instance.
> So what can I do?
>
> Thanks.

It works exactly the same, as the document I linked to shows. The
syntax you give should work with a standard ForeignKey - did you try
it?
--
DR.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.




Re: Questions about ContentType generic relations

2009-11-16 Thread Continuation

> As the docs show, when you've defined a GenericRelation, querying is
> exactly the same as with a reverse foreign key. So:
> Bookmark.objects.get(tags__tag='django')
>

A somewhat related question:

What if instead of having a ForeignKey to ContentType, TaggedItem has
a direct ForeignKey to Bookmark.

In that case is there something similar to GenericRelation that I can
define for Bookmark so that I can still use Bookmark.objects.get
(tags__tag='django') (or something similar) to find all bookmarks with
the tag "django"?

The doc said "You cannot access a reverse ForeignKey Manager from the
class; it must be accessed from an instance", but  in this case I
really need to start with the class Bookmark instead of an instance.
So what can I do?

Thanks.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.




Re: Questions about ContentType generic relations

2009-11-15 Thread Dennis Kaarsemaker
On zo, 2009-11-15 at 00:13 -0800, Continuation wrote:

> I checked out the doc (http://docs.djangoproject.com/en/dev/topics/db/
> queries/) and can't find reference to it. Can you explain its usage a
> bit more or point me to the relevant section of the doc?

http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

-- 
Dennis K.

The universe tends towards maximum irony. Don't push it.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.




Re: Questions about ContentType generic relations

2009-11-15 Thread Daniel Roseman
On Nov 15, 8:13 am, Continuation  wrote:
> > As the docs show, when you've defined a GenericRelation, querying is
> > exactly the same as with a reverse foreign key. So:
> > Bookmark.objects.get(tags__tag='django')
>
> I don't quite understand your use of tags__tag='django' in retrieving
> the objects. I've never seen this usage before.
>
> I checked out the doc (http://docs.djangoproject.com/en/dev/topics/db/
> queries/) and can't find reference to it. Can you explain its usage a
> bit more or point me to the relevant section of the doc?
>
> Thanks.

This is the way of crossing joins in Django lookups - for filters,
sorting, etc - and comes up pretty much everywhere. See here:
http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships
--
DR.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.




Re: Questions about ContentType generic relations

2009-11-15 Thread Continuation

>
> As the docs show, when you've defined a GenericRelation, querying is
> exactly the same as with a reverse foreign key. So:
> Bookmark.objects.get(tags__tag='django')
>


I don't quite understand your use of tags__tag='django' in retrieving
the objects. I've never seen this usage before.

I checked out the doc (http://docs.djangoproject.com/en/dev/topics/db/
queries/) and can't find reference to it. Can you explain its usage a
bit more or point me to the relevant section of the doc?

Thanks.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.




Re: Questions about ContentType generic relations

2009-11-14 Thread esatterwh...@wi.rr.com
You might want to do a search on django-tagging. this has been done
and done well.

On Nov 13, 6:04 pm, Continuation  wrote:
> How do I do queries when ContentType is involved?
>
> Using the example in the doc 
> (http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1
> )
>
> class TaggedItem(models.Model):
>     tag = models.SlugField()
>     content_type = models.ForeignKey(ContentType)
>     object_id = models.PositiveIntegerField()
>     content_object = generic.GenericForeignKey('content_type',
> 'object_id')
>
> class Bookmark(models.Model):
>     url = models.URLField()
>     tags = generic.GenericRelation(TaggedItem)
>
> So say I attached a bunch of tags to a bunch of bookmarks.
>
> How do I get a list of all bookmarks that have the tag "django"?
>
> Also what kind of SQL statements are generated behind the scene to
> enable this querying over generic relations?
>
> Say I have many different classes (e.g. Bookmark, ForumPost,
> WikiEntry, QnA, Message, Photo, etc) that I want to attach tags to.
>
> If I want to find all the bookmarks, forum posts, wiki entries, Q,
> messages, photos, etc that have the tag "django", can I do it in 1
> statement or would I have to do it separately for each class?
>
> Thanks

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.




Re: Questions about ContentType generic relations

2009-11-14 Thread Daniel Roseman
On Nov 14, 12:04 am, Continuation  wrote:
> How do I do queries when ContentType is involved?
>
> Using the example in the doc 
> (http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1
> )
>
> class TaggedItem(models.Model):
>     tag = models.SlugField()
>     content_type = models.ForeignKey(ContentType)
>     object_id = models.PositiveIntegerField()
>     content_object = generic.GenericForeignKey('content_type',
> 'object_id')
>
> class Bookmark(models.Model):
>     url = models.URLField()
>     tags = generic.GenericRelation(TaggedItem)
>
> So say I attached a bunch of tags to a bunch of bookmarks.
>
> How do I get a list of all bookmarks that have the tag "django"?
>
> Also what kind of SQL statements are generated behind the scene to
> enable this querying over generic relations?
>
> Say I have many different classes (e.g. Bookmark, ForumPost,
> WikiEntry, QnA, Message, Photo, etc) that I want to attach tags to.
>
> If I want to find all the bookmarks, forum posts, wiki entries, Q,
> messages, photos, etc that have the tag "django", can I do it in 1
> statement or would I have to do it separately for each class?
>
> Thanks

As the docs show, when you've defined a GenericRelation, querying is
exactly the same as with a reverse foreign key. So:
Bookmark.objects.get(tags__tag='django')

You can't get all the different objects tagged as 'django' in a single
go  - you would have to do:
[t.content_object for t in TaggedItems.objects.filter(slug='django')]

To find out what queries are being generated for this, set DEBUG=True
and look at django.db.connection.queries.
--
DR.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.




Questions about ContentType generic relations

2009-11-13 Thread Continuation
How do I do queries when ContentType is involved?

Using the example in the doc ( 
http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1
)

class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type',
'object_id')

class Bookmark(models.Model):
url = models.URLField()
tags = generic.GenericRelation(TaggedItem)

So say I attached a bunch of tags to a bunch of bookmarks.

How do I get a list of all bookmarks that have the tag "django"?

Also what kind of SQL statements are generated behind the scene to
enable this querying over generic relations?

Say I have many different classes (e.g. Bookmark, ForumPost,
WikiEntry, QnA, Message, Photo, etc) that I want to attach tags to.

If I want to find all the bookmarks, forum posts, wiki entries, Q,
messages, photos, etc that have the tag "django", can I do it in 1
statement or would I have to do it separately for each class?

Thanks

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.