On 7/30/06, Maciej Bliziński <[EMAIL PROTECTED]> wrote:

I'd like to find all the documents that are related to a specific new
tag. To reach the new tags, I need to pass old tags first, do I'd expect
to use something like this:

>>> Document.objects.filter (oldtags__newtags__content = 'popcorn')

But it doesn't work:
TypeError: Cannot resolve keyword 'newtags' into field

What you want here is:
 
>>> Document.objects.filter(oldtags__newtag__content = 'popcorn')

(i.e., newtag, not newtags)

The reason for this is the related name for the m2m objects.

Document literally has an oldtags field, so the first query term uses the name defined in the model. This puts the qeury context in the OldTag table.

The OldTag table is the 'remote' end of both m2m fields, so it gets m2m descriptors added automatically, using default names - in this case 'newtag_set' and 'document_set'. If you want to query over these fields, you omit the '_set' part of the default name (hence, oldtags__newtag__).

Alternatively, if you changed your model to define 'related_name's for the m2m fields:

class NewTag(models.Model):
  content = models.TextField()
  oldtags = models.ManyToManyField(OldTag, related_name='newtags')

class Document(models.Model):
   content = models.TextField()
   oldtags = models.ManyToManyField(OldTag, related_name='documents')

The descriptor and the query term use the same keyword - OldTags.newtags will be descriptor for the set of related new tags, and your original query:

>>> Document.objects.filter(oldtags__newtags__content = 'popcorn')

Will work as you expected - the query term looks for the explicitly defined related name, rather than the default name.

Hope this helps,

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to