如何使用关系创建 UpdateView CBV
Posted
技术标签:
【中文标题】如何使用关系创建 UpdateView CBV【英文标题】:How To Create UpdateView CBV with relationship 【发布时间】:2022-01-22 20:06:55 【问题描述】:我已经厌倦了寻找带有 CBV 的 UpdateView 的最佳方式。我测试了一些教程,但总是出错。也许有人可以帮助我。 我有2个模型。帐户和用户配置文件。 第一的。我这样注册帐户用户,并在 UserProfile 模型中扩展用户。 这就是成功。
UserProfile models
我的问题是,如何更新该字段的数据?
身份验证/models.py
class Account(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
full_name = models.CharField(max_length=150)
create_account = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_reviewer = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
objects = CustomAccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['full_name']
def __str__(self):
return self.email
dashboard/models.py
class UserProfil(models.Model):
JENIS_KELAMIN_CHOICE = (
('Pria', 'Pria'),
('Wanita', 'Wanita' ),
)
#Profil
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,)
gelar_depan = models.CharField(max_length=11, blank=True, default="")
gelar_belakang = models.CharField(max_length=20, blank=True, default="")
nik = models.CharField(max_length=11, blank=True, unique=True, default="")
nidn = models.CharField(max_length=11, blank=True, unique=True, default="")
email_alternatif = models.EmailField(_('email address'), blank=True, default="")
jenis_kelamin = models.CharField(max_length=6, blank=True, default="", choices =JENIS_KELAMIN_CHOICE)
tempat_lahir = models.CharField(max_length=30, blank=True, unique=True, default="")
tanggal_lahir = models.DateField(null=True, blank=True)
nomor_handphone = models.CharField(max_length=13, blank=True)
alamat = models.CharField(max_length=255, blank=True, default="")
dashboard/forms.py
class UserProfilForm(ModelForm):
class Meta:
model = UserProfil
fields = '__all__'
widgets =
'gelar_depan' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'gelarDepan', 'placeholder' : 'Gelar Depan'),
'user' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'namaLengkap', 'placeholder' : 'Nama Lengkap'),
'gelar_belakang' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'gelarBelakang', 'placeholder' : 'Gelar Belakang'),
'nidn' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'nidn', 'placeholder' : 'Nomor Induk Dosen Nasional'),
'nik' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'nik', 'placeholder' : 'Nomor Induk Karyawan'),
'tempat_lahir' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'gelarBelakang', 'placeholder' : 'Tempat Lahir'),
'tanggal_lahir' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'gelarBelakang', 'placeholder' : 'Tanggal Lahir', 'type' : 'date'),
'nomor_handphone' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'nik', 'placeholder' : 'No Handphone'),
'email_alternatif' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'emailAlternatif', 'placeholder' : 'Email Alternatif'),
'alamat' : forms.TextInput('class' : 'form-control form-control-user', 'id' : 'alamat', 'placeholder' : 'Alamat'),
'jenis_kelamin' : forms.Select('class' : 'form-control form-control-user', 'id' : 'jenisKelamin',),
dashboard/views.py
class UserProfilUpdateView(LoginRequiredMixin, UpdateView):
model = UserProfil
form_class = UserProfilForm
template_name = 'dashboard/profil.html'
success_url = '/dashboard/profil'
def form_valid(self, form):
# form.instance.user = self.request.user
messages.success(self.request, 'Success Save Your Data!.')
print(self.request.user)
return super().form_valid(form)
编辑
dashboard/views.py
class UserProfilUpdateView(LoginRequiredMixin, UpdateView):
model = UserProfil
fields = '__all__'
template_name = 'dashboard/profil.html'
queryset = UserProfil.objects.all()
success_url = reverse_lazy('dashboard/profil')
仪表板/urls.py
urlpatterns = [
path('', views.UserDashboardTemplateView.as_view(), name='index'),
path('profil/<pk>', views.UserProfilUpdateView.as_view(), name='profil'),
]
得到错误:raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch:反向'profil',没有找不到参数。尝试了 1 种模式:['dashboard/profil/(?P[^/]+)$']
【问题讨论】:
您的forms
有模特吗?
是的,先生,那个表格有一个模型。
@WillemVanOnsem 也许我可以得到一些文件先生?
【参考方案1】:
我得到了答案, 我需要 forms.py 中的两个类,并调用 Account 和 UserProfile 模型。
dashboard/forms.py
class UserAccountUpdateForm(ModelForm):
class Meta:
model = Account
fields = ['full_name', 'email']
class UserProfilUpdateForm(ModelForm):
class Meta:
model = UserProfil
exclude = ['user']
dashboard/views.py
class UserProfilUpdateView(LoginRequiredMixin, TemplateView):
model = UserProfil
u_form = UserAccountUpdateForm
p_form = UserProfilUpdateForm
template_name = 'dashboard/profil.html'
success_url = reverse_lazy('dashboard/profil')
def post(self, request):
post_data = request.POST or None
u_form = UserAccountUpdateForm(post_data, instance=request.user)
p_form = UserProfilUpdateForm(post_data, instance=UserProfil.objects.get(user = request.user))
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
context = self.get_context_data( u_form = u_form , p_form = p_form)
return self.render_to_response(context)
def get(self, request, *args, **kwargs):
return self.post(request, *args, **kwargs)
【讨论】:
以上是关于如何使用关系创建 UpdateView CBV的主要内容,如果未能解决你的问题,请参考以下文章
在 Django 中,如何在提交 CreateView 时重定向到 UpdateView?
如何在 Django 中将小部件添加到 UpdateView
Django - UpdateView ModelForm 在查询集字段上设置初始值
UpdateView 模板只显示一个相关模型的pk,如何让它显示其他字段?
python Django(极端情况):如何在FormView(CreateView / UpdateView)的form_valid方法中引发表单无效并添加错误消息