如何在 OneToOneField 中保存用户数据 |姜戈?
How save User data in OneToOneField | Django?
我有两个模型(User
和 Profile
)。在 Profile
模型中,如您所见,我有 idx
字段用于排序。在我的项目中,我显示了配置文件列表,管理员可以通过拖放对它们进行排序。这就是我使用 idx
的原因。此字段也是不可见的,管理员不会手动更改其值。
问题:如何通过配置文件 ID 编辑用户数据。正如我之前所说,我会显示配置文件列表。现在我有编辑表单,我在其中按个人资料 ID 显示用户数据。更改后尝试提交表单时出现问题。
models.py:
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
idx = models.IntegerField(verbose_name='Field for sorting, default=0, blank=True,)
class Meta:
ordering = ['idx', 'pk']
db_table = 'user_profile'
@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
@receiver(post_delete, sender=Profile)
def delete_user(sender, instance=None, **kwargs):
try:
instance.user
except User.DoesNotExist:
pass
else:
instance.user.delete()
forms.py:
from django import forms
from django.contrib.auth.models import User
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = '__all__'
views.py:
class UserEditView(UpdateView):
template_name = 'users/edit_user.html'
form_class = UserForm
model = User
def get(self, request, *args, **kwargs):
data = dict()
profile = Profile.objects.get(pk=self.kwargs['pk'])
user_edit_form = UserForm(instance=profile.user)
context = {
'profile': profile,
'user_edit_form': user_edit_form
}
data['html_user_edit_form'] = render_to_string(
'users/edit_user.html', context, request=request
)
return JsonResponse(data)
def form_valid(self, form):
form.save()
data = dict()
data['form_is_valid'] = True
context = {'users': User.objects.all()}
data['html_users'] = render_to_string('users/users.html', context)
return JsonResponse(data)
错误:
LevelName: WARNING | Module: base | Process: 662 | Thread: 123145371320320 | Message: Not Found: /user/31/edit/
LevelName: WARNING | Module: basehttp | Process: 662 | Thread: 123145371320320 | Message: "POST /user/31/edit/ HTTP/1.1" 404 1800
终于我发现了我的错误。
从 url 我获取个人资料的 ID,但随后在模板中我需要发送用户 ID。
我只是添加 context = {'user': profile.user,}
,然后在模板中使用 user.id
而不是 profile.id
。
我有两个模型(User
和 Profile
)。在 Profile
模型中,如您所见,我有 idx
字段用于排序。在我的项目中,我显示了配置文件列表,管理员可以通过拖放对它们进行排序。这就是我使用 idx
的原因。此字段也是不可见的,管理员不会手动更改其值。
问题:如何通过配置文件 ID 编辑用户数据。正如我之前所说,我会显示配置文件列表。现在我有编辑表单,我在其中按个人资料 ID 显示用户数据。更改后尝试提交表单时出现问题。
models.py:
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
idx = models.IntegerField(verbose_name='Field for sorting, default=0, blank=True,)
class Meta:
ordering = ['idx', 'pk']
db_table = 'user_profile'
@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
@receiver(post_delete, sender=Profile)
def delete_user(sender, instance=None, **kwargs):
try:
instance.user
except User.DoesNotExist:
pass
else:
instance.user.delete()
forms.py:
from django import forms
from django.contrib.auth.models import User
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = '__all__'
views.py:
class UserEditView(UpdateView):
template_name = 'users/edit_user.html'
form_class = UserForm
model = User
def get(self, request, *args, **kwargs):
data = dict()
profile = Profile.objects.get(pk=self.kwargs['pk'])
user_edit_form = UserForm(instance=profile.user)
context = {
'profile': profile,
'user_edit_form': user_edit_form
}
data['html_user_edit_form'] = render_to_string(
'users/edit_user.html', context, request=request
)
return JsonResponse(data)
def form_valid(self, form):
form.save()
data = dict()
data['form_is_valid'] = True
context = {'users': User.objects.all()}
data['html_users'] = render_to_string('users/users.html', context)
return JsonResponse(data)
错误:
LevelName: WARNING | Module: base | Process: 662 | Thread: 123145371320320 | Message: Not Found: /user/31/edit/
LevelName: WARNING | Module: basehttp | Process: 662 | Thread: 123145371320320 | Message: "POST /user/31/edit/ HTTP/1.1" 404 1800
终于我发现了我的错误。 从 url 我获取个人资料的 ID,但随后在模板中我需要发送用户 ID。
我只是添加 context = {'user': profile.user,}
,然后在模板中使用 user.id
而不是 profile.id
。