django userena编辑表单与相关的额外字段

时间:2012-11-27 08:35:13

标签: django forms django-forms django-userena

我有一些问题需要通过编辑django-userena的配置文件来解决。我想使用EditProfileFormExtra(它是userena EditProfileForm的扩展)来编辑Settings模型的相关额外字段(选择字段),它们与Accounts aplication处于OneToOne关系,字段为:default_house_system。

保存没问题,但default_house_system的初始值不起作用,始终显示元组的第一个值。我想要这样做而不在Django-Userena的原始文件中包含任何代码。

型号:

class UserProfile(UserenaBaseProfile):
    user = models.OneToOneField(User,
                            unique=True,
                            verbose_name='user',
                            related_name='user_profile')

    settings = models.OneToOneField(Settings, unique=True, verbose_name="Ustawienia", blank=True, null=True)

    is_virtual = models.BooleanField(default=False)

class Settings(models.Model):
    points = models.IntegerField(default=0)
    default_house_system = models.CharField(choices=HOUSE_SYSTEMS, max_length=24, default="Placidus")

Userena表格:

class EditProfileForm(forms.ModelForm):
""" Base form used for fields that are always required """
    first_name = forms.CharField(label=_(u'First name'),
                             max_length=30,
                             required=False)
    last_name = forms.CharField(label=_(u'Last name'),
                            max_length=30,
                            required=False)

    def __init__(self, *args, **kw):
        super(EditProfileForm, self).__init__(*args, **kw)
        # Put the first and last name at the top
        new_order = self.fields.keyOrder[:-2]
        new_order.insert(0, 'first_name')
        new_order.insert(1, 'last_name')
        self.fields.keyOrder = new_order

    class Meta:
        model = get_profile_model()
        exclude = ['user']

    def save(self, force_insert=False, force_update=False, commit=True):
        profile = super(EditProfileForm, self).save(commit=commit)
        # Save first and last name
        user = profile.user
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

我将url定义替换为accounts / urls.py:

url(r'^(?P<username>[\.\w]+)/edit/$',
   'userena.views.profile_edit', 
   {'edit_profile_form': EditProfileFormExtra},
   name='userena_profile_edit'),

...这里是EditProfileFormExtra的定义:

class EditProfileFormExtra(EditProfileForm):
    default_house_system = forms.ChoiceField(choices=HOUSE_SYSTEMS)

    def __init__(self, *args, **kw):
        super(EditProfileFormExtra, self).__init__(*args, **kw)

        new_order = self.fields.keyOrder[:-1]
        new_order.insert(3, 'default_house_system')
        self.fields.keyOrder = new_order

    class Meta:
        model = get_profile_model()
        fields = ['mugshot', 'default_house_system']

    def save(self, force_insert=False, force_update=False, commit=True):
        profile = super(EditProfileFormExtra, self).save(commit=commit)

        settings_profile = profile.settings
        settings_profile.default_house_system = self.cleaned_data['default_house_system']
        settings_profile.save()

    profile.save()

    return profile

现在重要的东西。如果我把:

'default_house_system':user.user_profile.settings.default_house_system

在userena / views.py / profile_edit方法的dict中(它是基于类的视图):

user_initial = {'first_name': user.first_name,
                'last_name': user.last_name,
                'default_house_system': user.user_profile.settings.default_house_system}

工作正常!但我不想在Django-Userena的原始文件中包含任何代码。

是否有另一种方法,而不是将整个profile_edit方法从userena / views.py复制到我的app accounts / views.py?

我很感激任何提示或建议,谢谢。

0 个答案:

没有答案