如何覆盖Django可重用应用程序的模型? [英] How to override a Django reusable app's model?

查看:83
本文介绍了如何覆盖Django可重用应用程序的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了可重复使用的Django应用(Django-Userena),并希望覆盖给定的models.py文件。

I've installed a django reusable app (Django-Userena) and would like to overwrite the given models.py file.

我创建了一个名为 accounts的应用,该应用从Django-Userena调用。在我的帐户应用程序中,我有一个models.py文件,该文件具有从Django-Userena类继承的类MyProfile-UserenaBaseProfile-类MyProfile(UserenaBaseProfile)

I have created an app named 'accounts' that calls from Django-Userena. In my 'accounts' app, I have this models.py file that has a class MyProfile that inherits from Django-Userena class UserenaBaseProfile - class MyProfile(UserenaBaseProfile)

UserenaBaseProfile类,有以下代码:

In the UserenaBaseProfile class, there is the following code:

privacy = models.CharField(_('privacy'),
                           max_length=15,
                           choices=PRIVACY_CHOICES,
                           default=userena_settings.USERENA_DEFAULT_PRIVACY,
                           help_text = _('Designates who can view your profile.'))

我想通过'editable = False'扩展隐私性,因为我不希望此字段自动显示生成的表单。

I would like to extend privacy with an extra value with 'editable=False,' as I do not want this field to be displayed in the auto-generated form.

我尝试了几种方法,例如使用新设置在MyProfile继承的模型中再次调用隐私,但是我只知道Django的字段名隐藏不允许(https://docs.djangoproject .com / zh-CN / 1.4 / topics / db / models /#field-name-hide-is-not-permitted)

I tried several ways like calling privacy again in the MyProfile inherited model with the new settings but I am only made aware of Django's "Field name "hiding" is not permitted" (https://docs.djangoproject.com/en/1.4/topics/db/models/#field-name-hiding-is-not-permitted)

我当前的解决方案是简单地将整个在调用下面的类MyProfile(UserenaBaseProfile)之前,请在帐户应用程序models.py中找到UserenaBaseProfile类。

My current solution is to simply include the whole UserenaBaseProfile class in my 'accounts' app models.py before calling class MyProfile(UserenaBaseProfile) below.

对我来说,这似乎不是一个优雅的解决方案。你们如何在可重用的应用程序中覆盖models.py文件?

This does not look like an elegant solution to me. How do you guys go about overriding the models.py file in the reusable app?

非常感谢。

推荐答案

我认为可以通过两种方式完成:

In my opinion it could be done in two ways:


  1. Make a fork of Django-Userena with your modified model and you use yours.

使用model.py创建Django-Userena的包装,然后使用包装器应用。

Make a wrapper of Django-Userena with your models.py and use your wrapper app.

对于urls.py/views.py,您可以输入:

For the urls.py/views.py you could just put:

#Your wrapper views:
from django-userena.views import *

#your wrapper urls:
from django-userena.urls import *

以下是您的模型:

#your MODIFIED model:
from django-userena.models import *
# then put you new UserenaBaseProfile

class UserenaBaseProfile(models.Model):
    #copy the model fields
    ...
    privacy = models.CharField(_('privacy'),
                       max_length=15,
                       choices=PRIVACY_CHOICES,
                       default=userena_settings.USERENA_DEFAULT_PRIVACY,
                       help_text = _('Designates who can view your profile.'))

然后,您可以在项目中使用自定义应用。

Then you could use your custom app in your project.

如果要自定义模板,在项目中创建一个模板目录,并在其中保留修改后的模板文件并保留其原始名称,以便django template-loader可以首先找到您的模板(这取决于在settings.py中配置模板加载器的方式)

If you want to customise templates, create a templates directory in your project and put there your modified template files keeping their original names, so the django template-loader could find yours first (it depends how template-loaders have been configured in your settings.py)

这篇关于如何覆盖Django可重用应用程序的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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