Python-在Django 2.2中更新用户 [英] Python - Update a user in Django 2.2

查看:54
本文介绍了Python-在Django 2.2中更新用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python(3.7)和Django(2.2)进行项目开发,在其中我通过扩展基本User模型为多种类型的用户实现了模型。现在,我需要实现一种允许管理员用户编辑用户的方法(不能使用默认的Django管理员)。因此,我利用了 MultiModelForm 来组合多个表单单个模板,在get请求中,表单已正确加载,并填充了数据。
这是我到目前为止所做的:

I'm working on a project using Python(3.7) and Django(2.2) in which I have implemented models for multiple types of users by extending the base User model. Now I need to implement a way to allow the admin user to edit a user ( can't use the default Django admin). So, I have utilized the MultiModelForm to combine multiple forms on a single template, on the get request the form is loading properly with data populated. Here's what I have done so far:

来自 models.py strong>

From models.py:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=254, unique=True)
    name = models.CharField(max_length=255)
    title = models.CharField(max_length=255, choices=TITLE, blank=False)
    user_type = models.CharField(max_length=255, choices=USER_TYPE, blank=False)
    gender = models.CharField(max_length=255, choices=CHOICES, blank=False)
    contenst = models.CharField(max_length=255, blank=True, null=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    last_login = models.DateTimeField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    email_status = models.CharField(max_length=50, default='nc')

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = ['password']

    objects = UserManager()

    def get_absolute_url(self):
        return "/users/%i/" % (self.pk)

    def __str__(self):
        return str(self.email)


class ContactPerson(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    contact_email = models.EmailField(blank=False)
    customer_id = models.BigIntegerField(blank=True)
    contact_no = PhoneNumberField(blank=True, null=True, help_text='Phone number must be entered in the'
                                                                   'format: \'+999999999\'. Up to 15 digits allowed.')
    collection_use_personal_data = models.BooleanField(blank=False)

以下是相关的 forms.py

Here's the related forms.py:

class EditUserForm(forms.ModelForm):
    password = None

    class Meta:
        model = User
        fields = ('email', 'name', 'title', 'gender', 'contenst',)

class UserCPForm(forms.ModelForm):
    class Meta:
        model = ContactPerson
        fields = ('customer_id', 'contact_email', 'contact_no', 'collection_use_personal_data')


class EditUserCPForm(MultiModelForm):
    form_classes = {
        'user': EditUserForm,
        'profile': UserCPForm
    }

这是我的相关 views.py

and here's my related views.py:

def post(self, request, *args, **kwargs):
    id = self.kwargs['id']
    if request.user.is_superuser:
        id = id
        u = User.objects.get(id=id)
        print(u.user_type)
        u_form = None
        elif u.user_type == 'ContactPerson':
            print(request.POST)
            u_form = EditUserCPForm(request.POST)
            # p_form = UserCPForm(request.POST['profile'])
            if u_form.is_valid():
                print('valid')
                user_form = u_form['user']
                profile_form = u_form['profile']
                ucp_obj = ContactPerson.objects.get(user__id=u.id)
                print(ucp_obj.user.email)
                ucp_obj.user.email = u_form.cleaned_data['user']['email']
                print(ucp_obj.user.email)
                ucp_obj.user.name = u_form.cleaned_data['user']['name']
                ucp_obj.user.title = u_form.cleaned_data['user']['title']
                ucp_obj.user.gender = u_form.cleaned_data['user']['gender']
                ucp_obj.user.contenst = u_form.cleaned_data['user']['contenst']
                ucp_obj.customer_id = u_form.cleaned_data['profile']['customer_id']
                ucp_obj.contact_email = u_form.cleaned_data['profile']['contact_email']
                ucp_obj.contact_no = u_form.cleaned_data['profile']['contact_no']
                ucp_obj.collection_use_personal_data = u_form.cleaned_data['profile']['collection_use_personal_data']
                ucp_obj.save()
                u_form.save(commit=False)
                print(ucp_obj.user.email)
                print(id)
                messages.success(request, 'success')
                return HttpResponseRedirect(reverse_lazy('dashboard:dashboard-home'))
            else:
                messages.error(request, 'not valid data')
                print(u_form.errors)
                return HttpResponseRedirect(reverse_lazy('dashboard:user-edit', u.id))

    else:
        messages.error(request, 'you are not allowed to do so!  ')
        return HttpResponseRedirect(reverse_lazy('dashboard:user-edit', id))




当我从模板中编辑任何字段并提交表单时,表单
会将我重定向到 dashboard-home ,其中成功消息,但数据库中的用户
没有更新。
这里有什么问题?

When I edit any field from the template and submit the form, the form redirects me to the dashboard-home with the success message but the user in the database is not updating. what can be wrong here?


推荐答案

我的意见是您未从数据库中选择用户,我将采用以下方式进行处理。

My opinion is your not selecting the user from the database, i would have approached this the following way.

user_form = u_form['user']
profile_form = u_form['profile']

ucp_obj = ContactPerson.objects.get(user__id=u.id)

u.email = u_form.cleaned_data['user']['email']
u.name = u_form.cleaned_data['user']['name']
u.title = u_form.cleaned_data['user']['title']
u.gender = u_form.cleaned_data['user']['gender']
u.contenst = u_form.cleaned_data['user']['contenst']
u.save()

ucp_obj.customer_id = u_form.cleaned_data['profile']['customer_id']
ucp_obj.contact_email = u_form.cleaned_data['profile']['contact_email']
ucp_obj.contact_no = u_form.cleaned_data['profile']['contact_no']
ucp_obj.collection_use_personal_data = u_form.cleaned_data['profile']['collection_use_personal_data']

ucp_obj.user = u
ucp_obj.save()

messages.success(request, 'success')
return HttpResponseRedirect(reverse_lazy('dashboard:dashboard-home'))

...
还需要更新EditUserCPForm实例。.

... Need to update EditUserCPForm instance as well..

    elif u.user_type == 'ContactPerson':
        print(request.POST)
        curr_user = User.objects.get(id=id)
        contact_person = ContactPerson.objects.get(user=u)
        print(u.user_type)
        u_form = EditUserCPForm(request.POST, instance={
            'user': curr_user,
            'profile': contact_person
        })

这篇关于Python-在Django 2.2中更新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆