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?
我如何为篮子模型渲染一个ModelForm,其中包含一个篮子的每个ProductQuantity的字段,只是为了能够修改其数量属性?
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?
如果我能用这样的ModelForm做这样的事情,我可以在admin.ModelAdmin中使用这个ModelForm作为替代表单属性在管理界面中具有相同的行为吗?
Edit :
@MuhammadTahir marked this post as possible duplicate of this post.
@MuhammadTahir将这篇文章标记为可能重复此帖子。
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,]
1 个解决方案
#1
0
Someone on IRC found the problem :
IRC上有人发现了这个问题:
inline
≠ inlines
in the admin.ModelAdmin
. And I don't even need the inlineformset_factory()
.
inline≠admin.ModelAdmin中的内联。我甚至不需要inlineformset_factory()。
So here is the final code :
所以这是最终的代码:
models.py
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)
admin.py
class ProductQuantityInline(admin.StackedInline):
model = ProductQuantity
fields = ["quantity",]
class BasketAdmin(admin.ModelAdmin):
inlines = [ProductQuantityInline,]
I hope this could help somebody else. :)
我希望这可以帮助别人。 :)
#1
0
Someone on IRC found the problem :
IRC上有人发现了这个问题:
inline
≠ inlines
in the admin.ModelAdmin
. And I don't even need the inlineformset_factory()
.
inline≠admin.ModelAdmin中的内联。我甚至不需要inlineformset_factory()。
So here is the final code :
所以这是最终的代码:
models.py
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)
admin.py
class ProductQuantityInline(admin.StackedInline):
model = ProductQuantity
fields = ["quantity",]
class BasketAdmin(admin.ModelAdmin):
inlines = [ProductQuantityInline,]
I hope this could help somebody else. :)
我希望这可以帮助别人。 :)