gpt4 book ai didi

django - 如何仅更新 django 模型中的特定字段?

转载 作者:行者123 更新时间:2023-12-04 09:46:06 27 4
gpt4 key购买 nike

我只想更新模型中的特定字段。
这是我的models.py

class Teacher(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(upload_to='teacher/images')
phone = models.CharField(max_length=15)
address = models.CharField(max_length=25)
salary = models.DecimalField(max_digits=20, decimal_places=2)
joindate = models.DateField(auto_now_add=True)
status = models.BooleanField(default=True)


这是我的 forms.py

class TeacherForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'username', 'email', 'password']


class TeacherExtraForm(forms.ModelForm):
class Meta:
model = models.Teacher
fields = ['phone', 'address', 'salary', 'status', 'image']


这是我的 views.py

def update_teacher(request, pk):
teacher = models.Teacher.objects.get(id=pk)
user = models.User.objects.get(id=teacher.user_id)
form1 = forms.TeacherForm(instance=user)
form2 = forms.TeacherExtraForm(instance=teacher)
context = {
'teacher': teacher,
'user': user,
'form1': form1,
'form2': form2
}
if request.method == 'POST':
form1 = forms.TeacherForm(request.POST, instance=user)
form2 = forms.TeacherExtraForm(request.POST, instance=teacher)
if form1.is_valid() and form2.is_valid():
user = form1.save(commit=False)

user.save(update_fields=['first_name', 'last_name', 'email', 'username'])
f2 = form2.save(commit=False)
f2.status = True
f2.save(update_fields=['phone', 'address', 'salary', 'status'])
return redirect('Teacher:teacher_index')
return render(request, 'Teacher/update_teacher.html', context)


这里我只想更新first_name、last_name、email、用户名、电话、地址、薪水和状态,而不影响密码和图像字段。当我也添加密码和图像字段时,它可以工作,但我不想在这里更新密码和图像。

最佳答案

通常在这种情况下,一种制作两种形式,一种用于创建,一种用于更新。例如,Django 本身有一个 UserCreateForm [Django-doc]和一个 UserChangeForm [Django-doc] .因此,我们可以构造两种额外的形式:

class TeacherUpdateForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'username', 'email']

class TeacherUpdateExtraForm(forms.ModelForm):
class Meta:
model = models.Teacher
fields = ['phone', 'address', 'salary', 'status']

在 View 中,我们因此使用这些形式:
from django.shortcuts import get_object_or_404

def update_teacher(request, pk):
teacher = get_object_or_404(models.Teacher, pk=pk)
user = teacher.user
if request.method == 'POST':
form1 = forms.TeacherUpdateForm(request.POST, instance=user)
form2 = forms.TeacherUpdateExtraForm(request.POST, instance=teacher)
if form1.is_valid() and form2.is_valid():
form1.save()
from2.instance.status = True
form2.save()
return redirect('Teacher:teacher_index')
else:
form1 = forms.TeacherUpdateForm(instance=user)
form2 = forms.TeacherUpdateExtraForm(instance=teacher)
context = {
'teacher': teacher,
'user': user,
'form1': form1,
'form2': form2
}
return render(request, 'Teacher/update_teacher.html', context)

关于django - 如何仅更新 django 模型中的特定字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62105654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com