我有以下两种模型
class Professor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
faculty_id = models.IntegerField(primary_key=True)
name = models.CharField(blank=True, max_length=50)
family_name = models.CharField(blank=True, max_length=50)
birth_date = models.DateField(blank=True, null=True)
national_id = models.IntegerField(blank=True, null=True)
....
并且:
class Journal(models.Model):
professor = models.ForeignKey(Professor, on_delete=models.CASCADE)
title = models.CharField(max_length=1000, null=True)
title_english = models.CharField(max_length=1000, null=True)
magazine_title = models.CharField(max_length=1000, null=True)
magazine_title_english = models.CharField(max_length=1000, null=True)
...
我有一个视图,列出了属于教授的所有期刊:
def journals_update(request):
professor = get_object_or_404(Professor,
user__username=request.user)
context = {"active_tab": "3-1", "active": "made-3",
"journals": Journal.objects.filter(professor=professor)}
return render(request, 'journals.html',
context=context)
我希望能够更改与journals.html中呈现的不同对象相关的字段,并保存用户发布时更改的字段。
我尝试了以下操作:
def post(self, request):
queryset = self.get_queryset()
form = JournalModelForm(request.POST)
if form.is_valid():
data = form.cleaned_data
print(data)
queryset.update(**data)
但是我意识到queryset.update()使用相同的值更新字段。 我该如何处理呢?我非常希望使用通用视图。