I have a Django form called MyDjangoModelForm
. In which, I have overridden the init()
function to make changes to the form based on the model. It looks like this:
def __init(self, *args, **kwargs)__:
super(MyDjangoModelForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
choice_items = models.MyChoiceItems.objects.filter(myfield=instance.myfield)
self.fields['choice_field'].widget.choices = [(choiceitem.id, choiceitem) for item in choice_items]
This works perfectly on my initial inline form. However when I click Add Another Inline Form
the entries it creates does not have the choices filter applied.
How can I apply this filter to the form that's added when I click Add another...
?