【发布时间】:2011-04-15 21:54:03
【问题描述】:
我的models.py:
USER_TYPES = (
('D', 'Demo' ),
('F', 'Free' ),
('P', 'Premium'),
)
class BaseProfile(models.Model):
user = models.OneToOneField(User, primary_key=True)
user_type = models.CharField(max_length=1, blank=True, choices=USER_TYPES)
class DemoProfile(models.Model):
user = models.OneToOneField(User, primary_key=True)
demo = models.CharField(max_length=10, blank=True)
...
class FreeProfile(models.Model):
user = models.OneToOneField(User, primary_key=True)
free = models.CharField(max_length=10, blank=True)
...
class PremiumProfile(models.Model):
user = models.OneToOneField(User, primary_key=True)
premium = models.CharField(max_length=10, blank=True)
...
class ProxyProfile(BaseProfile):
class Meta:
proxy = True
def get_profile(self):
if self.user_type == 'D':
return DemoProfile._default_manager.get(user__id__exact=self.user_id)
elif self.user_type == 'F':
return FreeProfile._default_manager.get(user__id__exact=self.user_id)
else:
return PremiumProfile._default_manager.get(user__id__exact=self.user_id)
我使用 BaseProfile 将 user_id 映射到特定的 user_type。我想使用 ProxyProfile 作为代理,它将依赖于用户类型的配置文件加载到 ModelForm,如下所示
我的 forms.py 的内容:
class ProfileForm(ModelForm):
...
class Meta:
model = ProxyProfile
exclude = ('user','user_type')
...
ProfileForm 使用 urls.py 中的以下代码提供给 django-profiles:
urlpatterns += patterns('',
url(r'^profiles/edit/', edit_profile,
{'form_class': ProfileForm},
name='profiles_edit_profile'),
(r'^profiles/',include('profiles.urls')),
)
我在settings.py中也设置了:
AUTH_PROFILE_MODULE = 'main.ProxyProfile'
在用户注册期间,所有数据库数据均已正确填写(看起来一切正常)。 我使用传递给 django-registration 的表单进行注册:
urlpatterns += patterns('',
url(r'^register/$', register,
{'form_class': UserRegistrationForm},
name='registration.views.register'),
(r'', include('registration.urls')),
)
来自forms.py:
class UserRegistrationForm(RegistrationFormUniqueEmail, RegistrationFormTermsOfService):
utype = forms.ChoiceField(choices=USER_CHOICES)
def save(self, profile_callback=None):
new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
password.self.cleaned_data['password1'],
email=self.cleaned_data['email'],
)
new_base_profile = BaseProfile(user=new_user, user_type=self.cleaned_data['utype'])
if self.cleaned_data['utype'] == "D":
new_profile = DemoProfile(user=new_user)
if self.cleaned_data['utype'] == "F":
new_profile = FreeProfile(user=new_user)
if self.cleaned_data['utype'] == "P":
new_profile = PremiumProfile(user=new_user)
new_profile.save()
new_base_profile.save()
return new_user
注册阶段工作正常。
我的个人资料编辑/详细信息页面有问题。 我的配置文件在 ProxyProfile 模型中过滤并用作 ProfileForm 中的 FormModel 未呈现(我看不到配置文件特定字段未呈现到 HTML 页面) 也许还有其他方式(更像是 Django 方式:))来做到这一点 (根据与用户模型相关的 user_type 字段选择和渲染配置文件模型)。
提前致谢:)
【问题讨论】:
-
我看到您使用了代理模型。有什么理由不能让 BaseProfile 成为抽象基类(请参阅docs.djangoproject.com/en/1.2/topics/db/models/…)以便 Django 为您加载正确的子类型?
-
我使用了基于 BaseProfile 的代理模型,因为我想覆盖 User 类的 get_profile 方法。 django-profiles 应用程序调用 get_profile() 以获取与用户关联的配置文件模型。问题是我对不同的用户有不同的配置文件模型(但一个用户 一种配置文件类型)。
标签: user-interface django-registration django-profiles