使用django-profiles以配置文件形式编辑相关模型 [英] Editing related models in profile form using django-profiles

查看:59
本文介绍了使用django-profiles以配置文件形式编辑相关模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了 django-profiles 给我一些简单的视图,可以帮助我更快地到达想要去的地方。

I'm using django-profiles in my app, as it gives me a few simple views that helps me get where I want to go, faster.

但是,我有一个问题。给定以下模型,如何创建用于编辑配置文件的表单,该配置文件包含 UserProfile 上的所有字段,中的first_name,last_name和email字段用户和一个或多个 PhoneNumber s?

However, I have one problem. Given the models below, how can I create a form for editing a profile that includes all the fields on UserProfile, the first_name, last_name and email fields from User, and one or more PhoneNumbers?

from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    height = models.IntegerField(_('Height'), max_length=3, blank=True, null=True, help_text=_('Users height in centimeters'))

    def get_absolute_url(self):
        return ('profiles_profile_detail', (), { 'username': self.user.username })
    get_absolute_url = models.permalink(get_absolute_url)

    def __unicode__(self):
        return self.user.username

class PhoneNumber(models.Model):
    description = models.CharField(_("Description"), max_length=32, blank=True)
    number = models.CharField(_("Phone number"), max_length=15)
    owner = models.ForeignKey(UserProfile, related_name="phone_numbers")

    def __unicode__(self):
        return u"%s (%s)" % (self.number, self.description)

我到目前为止管理的最接近的表单是包含 UserProfile 上所有字段以及 User 中的字段,并使用此处此处

The closest I've managed so far, is a form that includes all fields on UserProfile, and the wanted fields from User, using tricks explained here and here:

from django import forms
from main.models import UserProfile
from django.contrib.auth.models import User

class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email')

class ProfileForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        # magic
        self.user = kwargs['instance'].user
        user_kwargs = kwargs.copy()
        user_kwargs['instance'] = self.user
        self.uf = UserForm(*args, **user_kwargs)
        # magic end

        super(ProfileForm, self).__init__(*args, **kwargs)

        self.fields.update(self.uf.fields)
        self.initial.update(self.uf.initial)

    def save(self, *args, **kwargs):
        # save both forms
        self.uf.save(*args, **kwargs)
        return super(ProfileForm, self).save(*args, **kwargs)

    class Meta:
        model = UserProfile
        exclude = ("user",)

在管理员中,使用自定义ModelAdmin上的Inlines,我得到了想要的行为PhoneNumber s,但我无法以单一格式重新创建它以用于django配置文件。

In the admin, using Inlines on a custom ModelAdmin, I get the behaviour I want for the PhoneNumbers, but I've not been able to recreate it in a single form for use with django-profiles.

是吗还是我应该抛弃django-profiles并编写自己的profile-app,该应用程序可以使用表单集插入 PhoneNumber s?

Is it at all possible, or should I just ditch django-profiles and write my own profile-app that can use a formset to pull in the PhoneNumbers?

推荐答案

将两个模型形式塞入相同形式是相当扭曲的。选择:

that's quite a contortion to cram two modelforms into the same form. Choices:


  • 发表自己的看法。您可以利用Django URL的短路,先到先得的性质来盗版该URL。

  • Make your own view. You could utilize the short-circuit, first-come-first-serve nature of django urls to pirate that one.

将django配置文件引入您自己的版本中

Bring django-profiles into your own version control and hack it to your heart's content without the constraints of reusability.

您可以使用自己的保存方法创建一个大表格并使用django-profiles到位。这比仅声明 fields =('first_name','last_name','email') exclude =(用户,)请参阅文档字符串

You can just create the one big form with your own save method and use django-profiles in place. This would be more tedious than just being able to declare fields = ('first_name', 'last_name', 'email') and exclude = ("user",). See the docstrings

取决于项目。我绝对会写我自己的。个人档案应用的功能不多,您可能会遇到其他您希望拥有更多控制权的地方。

Depends on the project. I would definitely be tempted to write my own. There's not much to a profiles app and you may run into other places where you wish you had more control.

这篇关于使用django-profiles以配置文件形式编辑相关模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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