在Django 3.1中编辑用户模型数据

问题描述

除了标准Django字段外,我还具有带有个人资料图片字段的自定义用户模型。我也有用于创建用户自定义表单。现在,我想允许我的用户编辑他们的个人资料信息,因为这是我的第一个Django项目,而且我没有经验,所以我不知道如何以简洁的方式重复使用注册表格

private void menuNavigation() {
    bottomNavigation = (MeowBottomNavigation) findViewById(R.id.bottom);
    fragment_container = findViewById(R.id.fragment_container);
    bottomNavigation.add(new MeowBottomNavigation.Model(ID_HOME,R.drawable.ic_home));
    bottomNavigation.add(new MeowBottomNavigation.Model(ID_BLE,R.drawable.ic_bluetooth));
    bottomNavigation.add(new MeowBottomNavigation.Model(ID_ST,R.drawable.ic_heart));
    bottomNavigation.add(new MeowBottomNavigation.Model(ID_TML,R.drawable.ic_folder));

    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FragmentHome()).commit();
    bottomNavigation.setonClickMenuListener(new MeowBottomNavigation.ClickListener() {
        @Override
        public void onClickItem(MeowBottomNavigation.Model item) {
        }
    });

    bottomNavigation.setonReselectListener(new MeowBottomNavigation.ReselectListener() {
        @Override
        public void onReselectItem(MeowBottomNavigation.Model item) {
        }
    });

    bottomNavigation.setonShowListener(new MeowBottomNavigation.ShowListener() {
        @Override
        public void onShowItem(MeowBottomNavigation.Model item) {
            Fragment select_fragment = null;
            switch (item.getId()) {
                case ID_HOME:
                    select_fragment = new FragmentHome();
                    break;

                case ID_BLE:
                    select_fragment = new FragmentBLE();
                    break;

                case ID_TML:
                    select_fragment = new FragmentTML();
                    break;

                case ID_ST:
                    select_fragment = new FragmentST();
                    break;
            }
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,select_fragment).commit();
        }
    });

}

所以我去写另一种表格来编辑个人资料。

<activity android:name=".activitys.Home"
android:windowSoftInputMode="adjustPan"/>

让我感到困扰的是,我很乐意删除代码重复,但是不幸的是,我根本找不到用于用户编辑的任何好的示例,而且我不知道Django的处理方法是什么。第二件事是,如果我在总结时没有覆盖编辑表单的class CustomUserCreationForm(UserCreationForm): class Meta(UserCreationForm): model = CustomUser fields = UserCreationForm.Meta.fields + ('profile_picture',) username = forms.CharField(label='Enter Username',min_length=4,max_length=150) email = forms.EmailField(label='Enter email') first_name = forms.CharField(label='First name') last_name = forms.CharField(label='Last name') password1 = forms.CharField(label='Enter password',widget=forms.PasswordInput) password2 = forms.CharField(label='Confirm password',widget=forms.PasswordInput) profile_picture = forms.ImageField(label='Upload profile picture',required=False) ... def save(self,commit=True): user = CustomUser.objects.create_user( username=self.cleaned_data['username'],first_name=self.cleaned_data['first_name'],last_name=self.cleaned_data['last_name'],email=self.cleaned_data['email'],password=self.cleaned_data['password1'],profile_picture=self.cleaned_data['profile_picture'] ) return user ,它只会将更改应用于个人资料图片。例如。如果我尝试更改图片,则可以使用,但是如果我更改class CustomUserEditForm(forms.ModelForm): class Meta: model = CustomUser fields = UserCreationForm.Meta.fields + ('profile_picture',max_length=150,required=False) email = forms.EmailField(label='Enter email') first_name = forms.CharField(label='First name',required=False) last_name = forms.CharField(label='Last name',required=False) password1 = forms.CharField(label='Enter password',widget=forms.PasswordInput,required=False) password2 = forms.CharField(label='Confirm password',required=False) profile_picture = forms.ImageField(label='Upload profile picture',widget=forms.FileInput,required=False) def save(self,commit=True): user = self.instance if user.username != self.cleaned_data['username']: user.username = self.cleaned_data['username'] if user.first_name != self.cleaned_data['first_name']: user.first_name = self.cleaned_data['first_name'] ... user.save() return user ,则不能使用。它只是不更新​​名称值。这让我感到自己做错了非常严重的事情,因此我寻求建议和最佳实践指导以及可能的解释,以便更好地了解Django。

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)