0

ビュー.py

report = Report.objects.get(user=user.id)
reportnotesform=ReportNotes(instance=report)
if request.method == 'POST':
    locationnotesform=LocationNotes(request.POST,instance=report)
    if locationnotesform.is_valid():
        locationnotesform.save() 

フォーム.py

class LocationNotes(forms.ModelForm):
    other_location = forms.CharField(widget=forms.TextInput(attrs={'class':'ir-textbox'}))
    location_description = forms.CharField(widget=forms.Textarea(attrs={'style':'width:20em'}))

models.py

class Report(models.Model):
    user = models.ForeignKey(User, null=False)
    location_description = models.TextField('Location description', null=True, blank=True)
    other_location = models.CharField('Other', max_length=100, null=True, blank=True)

データを保存できました。フォームは更新モードです。

フィールド内のすべてのデータを削除して保存をクリックすると、フィールドが保存されず、null 値を取得していないことを意味します。

空白を節約しますが、null は節約できません。null 値も受け入れてほしいです。

4

2 に答える 2

1

私がそれを正しく理解していれば、あなたのLocationNotesフォームでは、作成する必要がありother_locationlocation_descriptionオプションでもあります:

other_location = forms.CharField(
    widget=forms.TextInput(attrs={'class':'ir-textbox'}),
    required=False)
于 2013-06-06T08:25:47.157 に答える
1

モデルは問題ありませんが、フォームでエラーが発生します。required=Falseフォームのフィールド定義に渡します。

class LocationNotes(forms.ModelForm):
    other_location = forms.CharField(required=False, widget=forms.TextInput(attrs={'class':'ir-textbox'}))
    location_description = forms.CharField(required=False, widget=forms.Textarea(attrs={'style':'width:20em'}))
于 2013-06-06T08:26:28.880 に答える