Python 使用ModelForm仅使用文本更新字段

Python 使用ModelForm仅使用文本更新字段,python,django,Python,Django,我有一个基于ModelForm的更新视图,我使用instance更新当前行,但问题是用户可能不需要更新所有字段。但是当表单保存数据时。它保存了所有内容,并为我提供了空白字段(用户没有填写)。这是我的密码: forms.py class EditYellowForm(forms.ModelForm): business_title = forms.CharField(required=False, widget=fo

我有一个基于
ModelForm
的更新视图,我使用
instance
更新当前行,但问题是用户可能不需要更新所有字段。但是当表单保存数据时。它保存了所有内容,并为我提供了空白字段(用户没有填写)。这是我的密码:

forms.py

class EditYellowForm(forms.ModelForm):
    business_title = forms.CharField(required=False,
                                     widget=forms.TextInput(
                                         attrs={
                                             'placeholder': 'New Title',
                                             'style': 'width: 100%; max-width: 500px'
                                         }
                                     )
                                     )

    contact_address = forms.CharField(required=False,
                                      widget=forms.TextInput(
                                          attrs={
                                              'placeholder': 'New Address',
                                              'style': 'width: 100%; max-width: 500px'
                                          }
                                      )
                                      )

    class Meta:
        model = YellowPages
        fields = ['business_title', 'contact_address']
def yellow_edit(request, pk):
    current_yellow_ad = get_object_or_404(YellowPages, pk=pk)
    if request.method == "POST":
        edit_yellow_form = EditYellowForm(instance=current_yellow_ad, data=request.POST, files=request.FILES)
        if edit_yellow_form.is_valid():
            instance = edit_yellow_form.save(commit=False)
            instance.save()
            return redirect('yellow_pages')
    else:
        edit_yellow_form = EditYellowForm(instance=current_yellow_ad, data=request.POST, files=request.FILES)

    context = {
        'edit_yellow_form': edit_yellow_form,
        'current_yellow_ad': current_yellow_ad,
    }
    return render(request, 'yellow_pages/edit.html', context)
视图.py

class EditYellowForm(forms.ModelForm):
    business_title = forms.CharField(required=False,
                                     widget=forms.TextInput(
                                         attrs={
                                             'placeholder': 'New Title',
                                             'style': 'width: 100%; max-width: 500px'
                                         }
                                     )
                                     )

    contact_address = forms.CharField(required=False,
                                      widget=forms.TextInput(
                                          attrs={
                                              'placeholder': 'New Address',
                                              'style': 'width: 100%; max-width: 500px'
                                          }
                                      )
                                      )

    class Meta:
        model = YellowPages
        fields = ['business_title', 'contact_address']
def yellow_edit(request, pk):
    current_yellow_ad = get_object_or_404(YellowPages, pk=pk)
    if request.method == "POST":
        edit_yellow_form = EditYellowForm(instance=current_yellow_ad, data=request.POST, files=request.FILES)
        if edit_yellow_form.is_valid():
            instance = edit_yellow_form.save(commit=False)
            instance.save()
            return redirect('yellow_pages')
    else:
        edit_yellow_form = EditYellowForm(instance=current_yellow_ad, data=request.POST, files=request.FILES)

    context = {
        'edit_yellow_form': edit_yellow_form,
        'current_yellow_ad': current_yellow_ad,
    }
    return render(request, 'yellow_pages/edit.html', context)
模型如果需要:

class YellowPages(models.Model):
    type_of_business_chioces = (
        ('service', 'service '),
        ('product ', 'product '),
    )
    business_title = models.CharField(max_length=120)
    contact_address = models.CharField(max_length=120, blank=True, null=True)
    contact_number = models.IntegerField(blank=True, null=True)
    contact_email = models.EmailField(blank=True, null=True)
    website = models.CharField(validators=[URLValidator()], max_length=120, blank=True, null=True)
    image = models.ImageField(upload_to=content_file_name, blank=True, null=True)
    category = models.ForeignKey(YelllowPagesCategory, on_delete=CASCADE)
    tags = TaggableManager()
    owned = models.BooleanField(default=False)
    owner = models.EmailField(blank=True, null=True)
    otp = models.IntegerField(default=0, blank=True, null=True)
    is_active = models.BooleanField(default=True)

如果我理解正确,您只想更新表单中不为空的字段。所以你可以使用论点。您应该将要更新的字段的名称作为列表传递。像这样的方法应该会奏效:

    if edit_yellow_form.is_valid():
        instance = edit_yellow_form.save(commit=False)
        form_data = edit_yellow_form.cleaned_data
        fields_to_update = [field for field in form_data if form_data[field] != '']
        instance.save(update_fields=fields_to_update)
        return redirect('yellow_pages')

不清楚你在问什么。如果用户可能未填写某些字段,而这些字段为空?您不希望发生这种情况吗?。如果您不希望出现这种行为,请相应地配置您的模型。但我认为你必须更好地解释你的问题。@guillermochamorro是的,这正是我的意思。项目已经列出,但是如果用户只需要更改标题怎么办。不是所有的信息!表单将更新
字段=
行中提到的所有内容。