简体   繁体   English

在单个项目中为多个应用程序使用django用户身份验证和taggit

[英]Using django user authentication & taggit for multiple apps in a single project

Background: I have 5 independent Django projects which I am attempting to combine in to 1 Django project composed of several apps. 背景:我有5个独立的Django项目,我试图将它们合并到由几个应用程序组成的1个Django项目中。 In other words: projA has appA, projB has appB & projC has appC, etc. I want 1 masterProj that has appA, appB & appC. 换句话说:projA有appA,projB有appB&projC有appC等我想要1个masterProj有appA,appB和appC。

Currently each app connects to it's own independent database (the apps don't share data). 目前,每个应用程序都连接到它自己的独立数据库(应用程序不共享数据)。 Each project uses Django user authentication, Django registration, taggit, profiles, comments and sorl-thumbnail. 每个项目都使用Django用户身份验证,Django注册,taggit,配置文件,注释和sorl-thumbnail。

I'm using Django 1.4 and setup database routing according to this stackoverflow answer so that, once combined into one project, each app in the newly combined Django project is still able to connect to its own database. 我正在使用Django 1.4并根据此stackoverflow答案设置数据库路由,这样,一旦合并到一个项目中,新组合的Django项目中的每个应用程序仍然能够连接到自己的数据库。 That went smoothly, but I started running into trouble with things like user authentication and taggit: 这很顺利,但我开始遇到用户身份验证和taggit等问题:

1) As mentioned before, each app connects to a different database and each of those databases has a table named 'auth_user'. 1)如前所述,每个应用程序连接到不同的数据库,每个数据库都有一个名为“auth_user”的表。 However, I've found that all read/write calls to the auth_user table (regardless of which app makes the read/write call) are routed to the default database (in this case appA's database): 但是,我发现对auth_user表的所有读/写调用(无论哪个应用程序进行读/写调用)都被路由到默认数据库(在本例中为appA的数据库):

# settings.py:
DATABASES['default'] = DATABASES['appA']
DATABASE_ROUTERS = ['appA.db.DBRouter', 'appB.db.DBRouter', 'appC.db.DBRouter']

# appA/dbrouterA.py (appB, appC routers are identical this, replacing 'appA' with 'appB', etc.)
class DBRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'appA':
            return 'appA'
        if model._meta.app_label == 'auth':
            return 'appA'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'appA':
            return 'appA'
        if model._meta.app_label == 'auth':
            return 'appA'
        return None

2) Assuming I get the routing working, if a user logs into appA, I don't want them to be logged into appB . 2) 假设我使路由工作,如果用户登录到appA,我不希望他们登录到appB I have seen many people post the reverse question (they want their apps to share user credentials) but has anyone successfully used Django user authentication in several independent apps in the same project? 我见过很多人发布了相反的问题(他们希望他们的应用程序共享用户凭据)但是有没有人在同一个项目中的几个独立应用程序中成功使用Django用户身份验证? If so, how did you do this? 如果是这样,你是怎么做到的?

3) I get the following error from my taggit code, but I haven't been able to figure out how to pass the "related_name" parameter to taggit. 3)我从我的taggit代码中得到以下错误,但我无法弄清楚如何将“related_name”参数传递给taggit。 I'm using the basic implementation of taggit - not subclassing anything: 我正在使用taggit的基本实现 - 而不是子类化任何东西:

# appA/models.py
tags = TaggableManager(blank=True)

# appB/models.py
tags = TaggableManager(blank=True)

Error: 错误:

appA.userprofile: Accessor for m2m field 'tagged_items' clashes with related m2m field 'TaggedItem.userprofile_set'. Add a related_name argument to the definition for 'tagged_items'.
appB.userprofile: Accessor for m2m field 'tagged_items' clashes with related m2m field 'TaggedItem.userprofile_set'. Add a related_name argument to the definition for 'tagged_items'.

4) I'm starting to get the feeling that combining all these apps is a slippery slope; 4)我开始觉得组合所有这些应用程序是一个滑坡; that later down the line I might run into problems with sorl-thumbnail or comments that haven't surfaced yet. 后来我可能会遇到没有浮出水面的sorl-thumbnail或评论的问题。 Has anyone successfully combined apps into a single project? 有没有人成功将应用程序组合到一个项目中? Or am I trying to do something that Django doesn't fundamentally support? 或者我是否尝试做一些Django没有从根本上支持的事情?

Thanks in advance for the help! 先谢谢您的帮助!

Django's architecture is designed to revolve around a Django project and several Django applications. Django的架构旨在围绕Django项目和几个Django应用程序展开。 The project itself is nothing more than your settings and main URL configuration module, while the applications are simple packages that follow a few file conventions. 项目本身只是您的设置和主URL配置模块,而应用程序是遵循一些文件约定的简单包。

Now, the applications themselves are never coupled to a particular project (they can be coupled to other applications by referencing them, though). 现在,应用程序本身永远不会耦合到特定项目(尽管可以通过引用它们将它们耦合到其他应用程序)。 The idea is to allow you to retain the freedom to design how your project's sources are structured and one approach that's common for most Django projects out there is to distribute Django applications under the project's top-level package, like most Python applications. 我们的想法是允许您保留设计项目源结构的自由,并且大多数Django项目都采用一种方法,即在大多数Python应用程序的项目顶层包中分发Django应用程序。 This approach makes it convenient to get a holistic view of all the features a project provides (when you apply meaningful application labeling), to create namespaces and provides the developers a convenient and organized path of access to particular project sources. 这种方法可以方便地获得项目提供的所有功能的整体视图(当您应用有意义的应用程序标签时),创建名称空间并为开发人员提供方便且有条理的访问特定项目源的途径。

This works great both for large projects and when you want to collocate and merge several different projects that reuse similar designs and approaches across. 这对于大型项目以及当您想要并置和合并多个不同的项目时都很有用,这些项目可以重复使用类似的设计和方法。 While this is going to affect only how your project is structured, choosing whether or not you'll have a configuration for a single Django project or several Django projects for your fixed set of Django applications has some important ramifications. 虽然这只会影响项目的结构,但是选择是否为单个Django项目配置一个配置,或者为您的固定Django应用程序设置几个Django项目会产生一些重要的后果。

When you create a Django project, your basically plugging in Django applications in to the framework's instrumentation and exposing application behavior, as we understand it in Web applications, by configuring and including mapping patterns for URLs and views from your Django applications. 当您创建Django项目时,您基本上将Django应用程序插入到框架的工具中并公开应用程序行为,正如我们在Web应用程序中所理解的那样,通过配置和包括来自Django应用程序的URL和视图的映射模式。

The point is that you can reorganize the sources in any way that works for you. 关键是您可以以适合您的任何方式重新组织源。 Your packages can be organized like proj.appA , proj.appB , etc. or proj.common1 , proj.common2 , proj.projA.app1 , proj.projA.app2 , proj.projB.app1 , it's really up to you. 你的包可以像proj.appAproj.appB等那样组织,或者proj.common1proj.common2proj.projA.app1proj.projA.app2proj.projB.app1 ,这真的取决于你。

What you should know is that you don't need a single settings and URL module and resort to database routing and managing database connections, you may just as well have a settings and URL module for each project, that reference different applications and expose different behavior. 您应该知道的是,您不需要单个设置和URL模块,并且需要使用数据库路由和管理数据库连接,您也可以为每个项目设置一个设置和URL模块,引用不同的应用程序并暴露不同的行为。 With per project database settings, your already reusing code and keeping the database data and state distinct for each project at the same time. 对于每个项目数据库设置,您已经重用的代码并同时保持每个项目的数据库数据和状态不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM