Preselective dynamic modelChoicefields Django
<p>I have 3 models, which are connected by Foreign Key like this</p>

<pre><code>models.py

class Partner(models.Model):
    partner_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    ...

class Product(models.Model):
    product_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    partner_id = models.ForeignKey(Partner)
    ...

class ProductData(models.Model):
    data_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    partner_id = models.ForeignKey(Partner)
    product_id = models.ForeignKey(Partner)
    ...
</code></pre>

<p>What I want is a form ProductDataForm, which adjust my choices for product_id by selection one partner_id. I had this forms.py above, but it doesn#t work :(</p>

<pre><code>forms.py

class ProductDataForm(ModelForm):
    partner_id = forms.ModelChoiceField(queryset=Partner.objects.all())
    product_id = forms.ModelChoiceField(queryset=Product.objects.filter(partner_id=partner_id)
</code></pre>

<p>I need to work with this dynamic for normal Users and Admins on Homeage and Adminsite.</p>

<p>Can someone help me? :)</p>

<p>I do not have a lot of experience with ModelForms or ModelChoiceField, but what I've done here might help:</p>

<pre><code>class ProductDataForm(forms.Form):
partner_id= forms.ChoiceField(
    choices = [ (u.id, u.id) for u in Partner.objects.all()]
    )
</code></pre>

python django