【问题标题】:Can I render an inline formset without data in an update view?我可以在更新视图中呈现没有数据的内联表单集吗?
【发布时间】:2016-03-18 18:28:07
【问题描述】:

我有这个模型:

class Picture(models.Model):
    image = models.ImageField(upload_to='media')
    show_name = models.CharField(max_length=128)
    file_name = models.CharField(max_length=128)

class PicturePrice(models.Model):
    picture = models.ForeignKey(Picture)
    price = models.PositiveSmallIntegerField()
    date = models.DateTimeField(auto_now_add=True)

对于这些模型,我想知道每次图片有新价格,以及添加每个价格的日期。像这样的:

Pictures: 1, 2, 3

Picture 1:
Price 5$ 3/16/2016
Price 3$ 3/18/2016

Picture 2:
Price 3$ 3/16/2016

Picture 3:
Price 5$ 3/16/2016
Price 3$ 3/17/2016
Price 4$ 3/18/2016

这样,我知道最新价格是多少,最新价格是多少。

我有这个价格的内联表单集:

price_formset=inlineformset_factory(
    Picture,
    PicturePrice,
    extra=0,
    min_num=1,
    can_delete=True,
    widgets={
    'price' : forms.TextInput(attrs={
        'class':'form-control price_per_pic',
        'maxlength' : 5
    })}
)

当我在视图中调用它时效果很好,但是当我尝试添加新价格时,它会显示一个带有先前价格的文本框,以便对其进行编辑。我不想编辑价格,我只想在数据库中添加一个新的寄存器。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: python django django-templates inline-formset


    【解决方案1】:

    尝试将price-formset 中的extra 值更改为大于0(您想要的空表单数)。因此,如果您想要 1 个空表单:

    price_formset_inlineformset_factory(
        ...
        extra=1,
        ...
    )
    

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 2017-12-29
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多