Answer the question
In order to leave comments, you need to log in
How to create a dynamic selection for M2M in the admin panel?
There are models:
class A(models.Model):
title = models.CharField()
class B(models.Model):
category = models.ForeignKey(A)
title = models.CharField()
class C(models.Model):
category = models.ForeignKey(A)
products = models.ManyToManyField(B)
class MyForm(forms.ModelForm):
class Meta:
model = C
fields = ('category', 'products')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
category = ???
self.fields['products'].queryset = category.products.all()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question