Select a subset of foreign key elements in inlineformset_factory in Django
Posted
by
Enis Afgan
on Stack Overflow
See other posts from Stack Overflow
or by Enis Afgan
Published on 2010-12-21T05:16:30Z
Indexed on
2010/12/21
5:21 UTC
Read the original article
Hit count: 431
Hello, I have a model with two foreign keys:
class Model1(models.Model):
model_a = models.ForeignKey(ModelA)
model_b = models.ForeignKey(ModelB)
value = models.IntegerField()
Then, I create an inline formset class, like so:
an_inline_formset = inlineformset_factory(ModelA, Model1, fk_name="model_a")
and then instantiate it, like so:
a_formset = an_inline_formset(request.POST, instance=model_A_object)
Once this formset gets rendered in a template/page, there is ChoiceField associated with the model_b field. The problem I'm having is that the elements in the resulting drop down menu include all of the elements found in ModelB table. I need to select a subset of those based on some criteria from ModelB. At the same time, I need to keep the reference to the instance of model_A_object when instantiating inlineformset_factory and, therefore, I can't just use this example. Any suggestions?
© Stack Overflow or respective owner