如何预填充 Django 更新表单并将其写回数据库 [英] How to prepopulate an Django Update form and write it back to the database

查看:21
本文介绍了如何预填充 Django 更新表单并将其写回数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手 &Django 并且目前正在苦苦挣扎.我使用 Django 模型表单创建了一个更新/编辑表单,但它只是没有预先填充表单字段并将其同时发布到数据库中.

I'm new at Python & Django and currently struggling right now. I created an update/edit form with Django Model forms, but it just doesn't prepopulate the form fields and post it to the database at the same time.

我认为问题在于form = AdvertForm(request.POST or None, request.FILES or None, instance=form).

如果没有request.POST or None, request.FILES or None,它会预填充字段,但不会将数据更新到数据库.

Without request.POST or None, request.FILES or None, it does prepopulate the fields but won't update the data to database.

这是我的views.py:

def update_advert(request, id):
if not request.user.is_authenticated():
    return render(request, 'forum/login.html')
else:
    form = get_object_or_404(Advert, pk=id)
    if request.method == 'POST':
        form = AdvertForm(request.POST or None, request.FILES or None, instance=form)
        if form.is_valid():
            form = form.save(commit=False)
            form.user = request.user
            form.save()
            return redirect('forum:user_account')
    else:
        form = AdvertForm(instance=form) 


    context = {'form': form}
    return render(request, 'forum/update_advert.html', context)

现在看起来像这样,当我尝试打开更新表单时:

In the moment it looks like this, when I try to open the update form:

打开表单 --> 未预填充 :(

推荐答案

根据 https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values,可以使用initial 实例化表单时的属性,以便预先生成您的表单.

According to https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values, you can use the initial attribute when instantiating forms in order to prepropulate your forms.

def update_advert(request, id):
    if not request.user.is_authenticated():
        return render(request, 'forum/login.html')
    else:
        advert_obj = get_object_or_404(Advert, pk=id)


        if request.method == 'POST':
            form = AdvertForm(request.POST or None, request.FILES or None, instance=advert_obj)
            if form.is_valid():
                form.save()
                return redirect('forum:user_account')
        else:
            # Prepopulation happens here:
            data = {"some_field": advert_obj.some_val} # Insert all the values of advert_obj here and their field names as keys.
            form = AdvertForm(initial=data) 


        context = {'form': form}
        return render(request, 'forum/update_advert.html', context)

在您的 AdvertForm 中,输入以下代码:

In your AdvertForm, put this code:

class AdvertForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(AdvertForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        instance = super(AdvertForm, self).save(commit=False)
        instance.user = self.request.user

        if commit:
            instance.save()
        return instance

覆盖的 save 方法只是执行您在视图中所做的将 request.user 链接到实例的操作,但是我已将该代码放在表单中类以保持您的 views 简单.

The overriding save method simply does what you were doing in the views to link up the request.user to the instance, however I have placed that code in the form class to keep your views simple.

此外,我可以看到一个令人困惑(但并不重要)的问题 - 您混淆了变量名称.当调用 form = get_object_or_404(Advert, pk=id) 时,这应该返回一个变量名,例如 advert 或类似的东西.不是 form ,因为这可能会令人困惑,因为我们返回的是模型对象而不是表单.类似地,form.save(commit=False) 返回一个实例"而不是模型表单.这不会解决您的问题,但应该指出,以便更清楚地了解返回的内容以及您应该如何命名变量.

Also, I can see one confusing (but not vital) issue - you have mixed up the variable names. When calling form = get_object_or_404(Advert, pk=id), this should return to a variable name such as advert or something similar. Not form as that can be confusing as we are returning a model object not a form. Similarly, form.save(commit=False) returns an "instance" not a model form. This won't solve your problem but should be pointed out for more clarification on what exactly is being returned and how you should then name your variables.

这篇关于如何预填充 Django 更新表单并将其写回数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆