【问题标题】:How to edit many objects at the same time in django?如何在 django 中同时编辑多个对象?
【发布时间】:2016-09-17 23:36:41
【问题描述】:

嗯,通常,当我喜欢编辑一个对象时,我会使用实例和 get_object_or_404,如下所示:

question = get_object_or_404(Question, id = id)
form = FormQuestion(request.POST, instance=question)
if  request.method == 'POST':
    if form.is_valid():
        form.save()
        return redirect('/Something/')
else:
    form = FormQuestion(instance = question)
return render(request, {'form':form}, 'Ask/question.html')

使用此代码我可以编辑一个对象,问题是:我需要列出所有问题并且字段将是可编辑的,但我找不到这样做的方法。

有人有解决我问题的方法或代码吗?

【问题讨论】:

标签: python django edit


【解决方案1】:

首先,你在 CBV(基于类的视图)中开发得更好,在那里你会更容易地做视图。

我遇到了您的问题,请按照以下方法操作: Django - inlineformset-factory (How to Edit) 你需要 formset-factory 和/或 inlineformset-factory。 在这里:http://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_formsets.htm

https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-more-than-one-formset-in-a-view

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多