Assuming I have :
class Product(models.Model):
[...]
class Basket(models.Model):
content = models.ManyToManyField(Product, through="ProductQuantity")
class ProductQuantity(models.Model):
basket = models.ForeignKey(Basket)
product = models.ForeignKey(Product)
quantity = models.IntegerField(default=0)
How could I render a ModelForm
for the Basket
Model with a field for each ProductQuantity
of a Basket
, just to be able to modify its quantity
attribute?
Is there a widget I could use for this?
If I was able to do such thing with such a ModelForm, could I use this ModelForm
in an admin.ModelAdmin
as an alternative form
attribute to have the same behaviour in the admin interface?
Edit :
@MuhammadTahir marked this post as possible duplicate of this post.
It indeed helped me to understand better, but I'm still stuck : I can't render the fields I want to render.
Here is my code so far :
models.py
Same as above.
forms.py
ProductQuantityFormSet = inlineformset_factory(Product,
basket.content.through,
fields=("quantity",))
admin.py
class ProductQuantityInline(admin.StackedInline):
model = ProductQuantity
formset = ProductQuantityFormSet()
class BasketAdmin(admin.ModelAdmin):
inline = [ProductQuantityInline,]