Note that there are some explanatory texts on larger screens.

plurals
  1. POReverse foreign key to django-fts searchable model in template
    primarykey
    data
    text
    <p>I have a pair of Django models with a foreign key, one of which is searchable with django-fts, say:</p> <pre><code>class Foo(models.Model): ... class Bar(fts.SearchableModel): foo = models.ForeignKey(Foo) </code></pre> <p>When I have an instance of Foo, in view I can insert <code>print foo.bar_set.all()</code> and I see an array of results. However if I try to use it in a view, in any of following ways:</p> <pre><code>{{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 %} </code></pre> <p>and literally <em>any</em> construct that I can think of behaves as if <code>foo</code> instance had no <code>bar_set</code> attribute.</p> <p>Edit: I am sure that I have a <code>Foo</code> instance in the template, I tested following to work as expected:</p> <pre><code>{{foo|pprint}} {{foo.id}} (and any other simple attributes of Foo) </code></pre> <p>I am sure there are related Bar objects, as I check that from view (<code>print foo.bar_set.all()</code>). And if the QuerySet was empty, <code>{{foo.bar_set.all|pprint}}</code> would yield <code>[]</code>, not <code>''</code> (which it does on <code>{{foo.bar_set.all|pprint}}</code>, <code>{{foo.bar_set|pprint}}</code>, and any <code>{{foo.nonexistent_attribute|pprint}}</code>).</p> <p>This behaviour started when I moved development from SQLite database to PostgreSQL, with psycopg2 driver, to use django-fts full text search.</p> <p>I could not find any other answer, because googling this is very hard: "reverse relationship" or "reverse foreign key" is all littered with unrelated <code>django.core.urlresolvers.reverse</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload