[Answer]-Inverted foreign-key relation in django admin

1👍

You can address the Foo instances using

bar = Bar.objects.get(pk = bar_id)
foo_set = bar.foo_set.all()

where bar_id is the primary key of your Bar object, or alternatively

foo_set = Foo.objects.filter(bar__pk = bar_id) # Note the double underscore

If you want to render the Foo objects in a select tag in a template you could either do it manually:

<select>
{% for f in foo_set %}
<option value="{{ f.pk }}">{{ f }}</option>
{% endfor %}
</select>

Or you could create a django form, see django forms.

👤kreld

Leave a comment