Add new QuerySet to reverse of a ManyToMany relation

30 views
Skip to first unread message

Arruda

unread,
Apr 15, 2012, 11:08:03 PM4/15/12
to django...@googlegroups.com
Hi there, I don't know if I put the right title, so here is what I'm stuck with:
I have this model:
class Foo(models.Model):
    name = models.CharField(...) 

this one:
class Bar(models.Model):
     name = models.CharField(....)

And their connection:
class FooBar(models.Model):
    foo = models.ForeingKey(Foo, related_name='bars')
    bar = models.ForeingKey(Bar, related_name='foos')
    some_other_value = ..... 

And wanted to know how can I(if possible) do something that has the same result as this code:
f = Foo.objects.get(name='some foo')
b = Bar.objects.get(name='nice bars')
my_nice_bars = f.bars.filter(bar = b)

but without having to do the "b = Bar.objects.get(name='nice bars')" like this:
f = Foo.objects.get(name='some foo')
my_nice_bars = f.bars.get_by_name('nice bars')

Or even do a more complex query then "get_by_name".
Can I add this new query to the Foo.bars stuff?

I tried the QueryManager from django-model-utils, and added a new query to Bar, but this couldn't be used in Foo.bars


Arruda

unread,
Apr 15, 2012, 11:31:30 PM4/15/12
to django...@googlegroups.com
Sorry everyone, I just found out this was a fixed issue in the PassThroughManager of django-model-utils, in 1.1.0 it adds also the queryset to the related.
=)
Reply all
Reply to author
Forward
0 new messages