I have a pair of Django models with a foreign key, one of which is searchable with django-fts, say:
class Foo(models.Model):
...
class Bar(fts.SearchableModel):
foo = models.ForeignKey(Foo)
When I have an instance of Foo, in view I can insert print foo.bar_set.all()
and I see an array of results. However if I try to use it in a view, in any of following ways:
{{foo.bar_set|pprint}}
{{foo.bar_set.all|ppring}}
{{foo.bar_set.count}}
{{foo.bar_set.all|length}}
{% for bar in foo.bar_set.all %} {{bar}} {% endfor %}
and literally any construct that I can think of behaves as if foo
instance had no bar_set
attribute.
Edit: I am sure that I have a Foo
instance in the template, I tested following to work as expected:
{{foo|pprint}}
{{foo.id}} (and any other simple attributes of Foo)
I am sure there are related Bar objects, as I check that from view (print foo.bar_set.all()
). And if the QuerySet was empty, {{foo.bar_set.all|pprint}}
would yield []
, not ''
(which it does on {{foo.bar_set.all|pprint}}
, {{foo.bar_set|pprint}}
, and any {{foo.nonexistent_attribute|pprint}}
).
This behaviour started when I moved development from SQLite database to PostgreSQL, with psycopg2 driver, to use django-fts full text search.
I could not find any other answer, because googling this is very hard: "reverse relationship" or "reverse foreign key" is all littered with unrelated django.core.urlresolvers.reverse
references, and I have no idea how to name "*_set" thing to Google. A hint on how to google this one would be helpful as well.