【问题标题】:Post method in Django modelsDjango 模型中的 Post 方法
【发布时间】:2016-03-17 07:38:20
【问题描述】:

我是 Django 表单的新手,我需要将一些数据插入/更新到我的数据库中。

我有一些模型,在 django 管理面板中我手动介绍了用户的电话和 IMEI 号码。 之后,我创建了一个表单、一个 template.html 和一个视图。

表格如下:

from django import forms

class Send2phone(forms.Form):

    NumberOfCalls = forms.CharField(
        min_length=1,
        widget=forms.TextInput({'class': 'form-control'})
        )

    TimeBetweenCalls = forms.CharField(
        widget=forms.TextInput({'class': 'form-control'})
        )

    PSAP = forms.CharField(
        min_length=1,
        widget=forms.TextInput({'class': 'form-control'})
        )

我的看法是:

def phone_config(request):

    if request.method == 'POST':
        form = Send2phone(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            NumberOfCalls = cleaned_data.get('NumberOfCalls')
            TimeBetweenCalls = cleaned_data.get('TimeBetweenCalls')
            PSAP = cleaned_data.get('PSAP')
            phone_model = Phone()
            phone_model.id = 1
            phone_model.user = donothing
            phone_model.imei = donothing
            phone_model.num_calls = NumberOfCalls
            phone_model.time_btwn_calls = TimeBetweenCalls
            phone_model.psap = PSAP
            phone_model.save()
            return redirect(reverse('gracias'))
    else:
        form = Send2phone()
    return render(request, 'heroconfigurer/heroconfigurer.html', {'form': form})


def gracias_view(request):
    return render(request, 'heroconfigurer/gracias.html')

当我创建视图时,我的问题就出现了。首先,我检查该方法是否已发布,并从表单中获取数据。 然后我检查表单是否有效并创建对象电话。之后分配不同的参数并保存它们。 从表单中插入数据效果很好,但如果我没有指定imei和用户ara,它们就会被删除。

如何在存在一些用户和imeis的数据库模型中插入数据?例如,在 id=1 中,我已经有一个用户和一个imei,我想保留它们

【问题讨论】:

  • 你需要在模型上使用update方法,而不是save方法。

标签: python django forms django-models django-views


【解决方案1】:

现有字段被删除,因为您正在使用现有 ID 创建新的 Phone 对象。您应该改为检索现有的 Phone 模型并更新它:

        phone_model = Phone.objects.get(id=1)
        phone_model.num_calls = NumberOfCalls
        phone_model.time_btwn_calls = TimeBetweenCalls
        phone_model.psap = PSAP
        phone_model.save()

或者使用查询集更新方法更新它:

        Phone.objects.filter(id=1).update(
            num_calls=NumberOfCalls,
            time_btwn_calls=TimeBetweenCalls,
            psap=PSAP,
        )

第一个将触摸数据库两次。一次用于加载现有电话,然后用于保存新值。 Second 将只触及数据库一次,更新字段而不触及其余字段。

【讨论】:

    【解决方案2】:

    您应该检索现有的 Phone 对象并在必要时对其进行更新。

        if form.is_valid():
            number_of_calls = form.cleaned_data.get('NumberOfCalls')
            time_between_calls = form.cleaned_data.get('TimeBetweenCalls')
            psap = form.cleaned_data.get('PSAP')
            phone = Phone.objects.get(pk=1)
            phone.num_calls = number_of_calls
            phone.time_btwn_calls = time_between_calls
            phone.psap = psap
            phone.save()
    

    更好的是,让您的表单成为 ModelForm,并且只包含您要更新的字段:

    class Send2phone(forms.ModelForm):
        class Meta:
            model = Phone
            fields = ['num_calls', 'time_btwn_calls', 'psap']
    

    现在你的观点是:

    phone = Phone.objects.get(pk=1)
    if request.method == 'POST':
        form = Send2tcu(request.POST, instance=phone)
        if form.is_valid():
            form.save()
            return redirect(reverse('gracias'))
    else:
        form = Send2tcu(instance=phone)
    return render(request, 'heroconfigurer/heroconfigurer.html', {'form': form})
    

    【讨论】:

    • 您好丹尼尔,感谢您的评论。这样做是可行的,并且会显示 textInput num 调用、时间 btwn 调用和 psap。为了以正确的方式显示文本,如何将 num 调用显示为:调用次数和时间 btwn 调用为:调用之间的时间?
    • 您可以在 Meta 类中使用labels dict,参见documentation
    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2017-11-12
    • 2018-02-15
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多