这两种使用表单更新数据库的方法有什么区别?
What is difference between these two method of updating DB using form?
我正在尝试使用表单更新数据库。我想 select 下拉列表中的标题,并更新 'opening_crawl' 字段以从文本区域输入。
models.py :
class Movies(models.Model):
episode_nb = models.IntegerField(primary_key=True)
title = models.CharField(max_length=64, unique=True, null=False)
opening_crawl = models.TextField(null=True)
director = models.CharField(max_length=32)
producer = models.CharField(max_length=128)
release_date = models.DateField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True, editable=True)
forms.py:
class TitleDropDownForm(forms.Form):
title = forms.ModelChoiceField(queryset=Movies.objects.only('title'), empty_label=None)
opening_crawl = forms.CharField(widget=forms.Textarea)
views.py:
def update(request):
msg = ''
if request.method == 'POST':
form = TitleDropDownForm(request.POST)
if form.is_valid():
#method 1 : it updates 'opening_crawl' properly, but not 'updated_time'.
movie = form.cleaned_data['title']
movie.opening_crawl = form.cleaned_data['opening_crawl']
movie.save()
#method 2
#h = Movies.objects.get(pk=1)
#h.opening_crawl = 'HAND WRITTEN MESSAGE!'
#h.save()
return redirect(request.META.get('HTTP_REFERER'))
else:
form = TitleDropDownForm()
if not form.fields['title'].queryset:
msg = 'No data available.'
return render(request, 'ex07/update.html', context={'form' : form, 'msg' : msg})
方法 1 适用于 'opening_crawl' 字段,但 'updated' 日期时间字段未更改。
当我像方法 2 一样尝试时,它会正确更新两个字段。
两种方法有什么区别?有什么误会吗?
我怀疑是因为你在使用 .only()
When saving a model fetched through deferred model loading (only() or defer()) only the fields loaded from the DB will get updated. In effect there is an automatic update_fields in this case. If you assign or change any deferred field value, the field will be added to the updated fields.
在第二种方法中,您将获得整个模型,没有任何延迟字段(即 updated
)
我正在尝试使用表单更新数据库。我想 select 下拉列表中的标题,并更新 'opening_crawl' 字段以从文本区域输入。
models.py :
class Movies(models.Model):
episode_nb = models.IntegerField(primary_key=True)
title = models.CharField(max_length=64, unique=True, null=False)
opening_crawl = models.TextField(null=True)
director = models.CharField(max_length=32)
producer = models.CharField(max_length=128)
release_date = models.DateField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True, editable=True)
forms.py:
class TitleDropDownForm(forms.Form):
title = forms.ModelChoiceField(queryset=Movies.objects.only('title'), empty_label=None)
opening_crawl = forms.CharField(widget=forms.Textarea)
views.py:
def update(request):
msg = ''
if request.method == 'POST':
form = TitleDropDownForm(request.POST)
if form.is_valid():
#method 1 : it updates 'opening_crawl' properly, but not 'updated_time'.
movie = form.cleaned_data['title']
movie.opening_crawl = form.cleaned_data['opening_crawl']
movie.save()
#method 2
#h = Movies.objects.get(pk=1)
#h.opening_crawl = 'HAND WRITTEN MESSAGE!'
#h.save()
return redirect(request.META.get('HTTP_REFERER'))
else:
form = TitleDropDownForm()
if not form.fields['title'].queryset:
msg = 'No data available.'
return render(request, 'ex07/update.html', context={'form' : form, 'msg' : msg})
方法 1 适用于 'opening_crawl' 字段,但 'updated' 日期时间字段未更改。 当我像方法 2 一样尝试时,它会正确更新两个字段。 两种方法有什么区别?有什么误会吗?
我怀疑是因为你在使用 .only()
When saving a model fetched through deferred model loading (only() or defer()) only the fields loaded from the DB will get updated. In effect there is an automatic update_fields in this case. If you assign or change any deferred field value, the field will be added to the updated fields.
在第二种方法中,您将获得整个模型,没有任何延迟字段(即 updated
)