I have a model structure along those lines:
# models.py
class Foo(models.Model):
...
class Bar(models.Model):
foo = models.ForeignKey(Foo)
...
class Baz(models.Model):
bar = models.ForeignKey(Bar)
...
Now, on the DetailView
for Foo
instances, I have a link to create Baz
instances that belong to the Foo
instance (via one of its Bar
instances).
That Link points to the following CreateView
:
class BazCreateView(CreateView):
model = Baz
fields = ['bar', ...]
As the Baz Instance to be created shall belong to one of foo's Bar instances, I'd like to limit the choices for that field to the Bar instances belonging to foo.
So far, I've found ForeignKey.limit_choices_to
which seems to be for use in the Model and
This question
which is 9 years old and thus probably has an outdated answer. If it isn't outdated I'd like some advice on how to access the form
object mentioned in the accepted answer and how to pass the Bar
id to the CreateView.