如何在Django中使用模型的外部字段设置字段的初始值?

时间:2019-06-17 01:33:52

标签: django django-forms

我希望以配置​​文件形式设置初始值。

个人资料模型与用户模型具有OneToOne关系。

init 方法中设置初始字段时,出现以下错误

  

'ForwardOneToOneDescriptor'对象没有属性'email'

跟踪

  

文件   内部的“ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/core/handlers/exception.py”     34. response = get_response(request)

     

文件   “ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/core/handlers/base.py”   在_get_response中     126. response = self.process_exception_by_middleware(e,request)

     

文件   “ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/core/handlers/base.py”   在_get_response中     124. response = wrapd_callback(request,* callback_args,** callback_kwargs)

     

文件   “ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/views/generic/base.py”   鉴于     68. return self.dispatch(request,* args,** kwargs)

     

文件   “ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/views/generic/base.py”   在派遣中     88. return handler(请求,* args,** kwargs)

     

文件   “ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/views/generic/edit.py”   在得到     190. return super()。get(request,* args,** kwargs)

     

文件   “ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/views/generic/edit.py”   在得到     133. return self.render_to_response(self.get_context_data())

     

文件“ /home/drogon/Crowdsocial_project/users/views.py”位于   get_context_data     90. context = super(ProfileSettingsView,self).get_context_data(** kwargs)

     

文件   “ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/views/generic/edit.py”   在get_context_data中     66. kwargs ['form'] = self.get_form()

     

文件   “ /home/drogon/Crowdsocial_project/venv_ubuntu/lib/python3.6/site-packages/django/views/generic/edit.py”   在get_form中     33. return form_class(** self.get_form_kwargs())

     

初始化中的文件“ /home/drogon/Crowdsocial_project/users/forms.py”     144. self.initial ['profile_email'] = Profile.user.email

     

异常类型:/ users / 12 / profile / settings上的AttributeError异常   值:'ForwardOneToOneDescriptor'对象没有属性'email'

个人资料模型

class Profile(models.Model):
    user            = models.OneToOneField(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
    full_name       = models.CharField(max_length=30, null=True, blank=True)

个人资料表格

class ProfileSettingsForm(forms.ModelForm):

    full_name      = forms.CharField(required=False,
                                    widget=forms.TextInput(attrs={'readonly': 'readonly'}))
    profile_email  = forms.CharField(required=False,
                                    widget=forms.TextInput(attrs={'readonly': 'readonly'}))

    def __init__(self, *args, **kwargs):
        super(ProfileSettingsForm, self).__init__(*args, **kwargs)

        self.initial['profile_email'] = Profile.user.email
        self.initial['full_name'] = Profile.user.name

    class Meta:
        model = Profile

        fields = ['image','full_name','biography','profile_email','linked_in','facebook',
                  'twitter','phone','education']

0 个答案:

没有答案