Using django user authentication & taggit for multiple apps in a single project
Asked Answered
G

1

8

Background: I have 5 independent Django projects which I am attempting to combine in to 1 Django project composed of several apps. In other words: projA has appA, projB has appB & projC has appC, etc. I want 1 masterProj that has 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.

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. That went smoothly, but I started running into trouble with things like user authentication and taggit:

1) As mentioned before, each app connects to a different database and each of those databases has a table named '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):

# 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. 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? 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. I'm using the basic implementation of taggit - not subclassing anything:

# 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; that later down the line I might run into problems with sorl-thumbnail or comments that haven't surfaced yet. Has anyone successfully combined apps into a single project? Or am I trying to do something that Django doesn't fundamentally support?

Thanks in advance for the help!

Germin answered 24/5, 2012 at 21:52 Comment(6)
Why do you want to combine them? It sounds to me that you actually want to keep them separate -- even the auth table is not shared. Anyhow, you'd have to at least build a new authentication backend that actually uses using() in the get call as well as a session handler that knows which app you're logged into... My gut tells me this is very unexplored territory. Good luck!Cretin
My end goal is to consolidate the apps (meaning 1 Django project with 1 app; currently a lot of the apps share common code). But as a first step I thought I would combine them all into 1 project; however, this is getting much tougher than I thought. Thanks for the info!Germin
hmm interesting. Perhaps you could start to tackle that project by dealing with authentication in the default database as it would be in the final combined product? Your code would not have to change from the intermediary stage (5 app) to the final (1 app) when you combine databases if you come up with a auth interface that would handle separate logins for different sections of the site! Also one tip: you would probably get more help if you separate your auth question from your taggit question.Cretin
The taggit source didn't show a related_name and it appears to be an open issue: github.com/alex/django-taggit/issues/50. There's a fix to pass it a related name in there.. field.rel.related_name='+'. Your target python instance needs a unique attribute to resolve the reverse lookup model. Setting it to + will remove the reverse lookup helper.Cretin
I modified my models to each pass related_name: tags = TaggableManager(blank=True) tags.rel.related_name = "appA" - that does solve the problem of the tags variable clashing, but unfortunately not the tagged_items variable; I still get the same errors & I can't figure out how to pass related_name to that variable. Also: thanks for feedback re:consolidation and creating a single auth interface; I'm definitely leaning towards doing that now :) This is my first stackoverflow post so thx for the tip to separate questions next time.Germin
Where is the relationship between UserProfile and TaggedItem defined? It seems like your appA/appB UserProfile models have an FK/M2M to TaggedItem via however taggit works, and you'll have to set the related_names there. I'm sorry how taggit works is still a little confusing to me. All of the tag models for 1.2+ have unique related names per app, so I'm not sure where this FOO_set is coming from. Yes, if you post separate questions you might get answer attempts whereas here I have no confidence that I'm actually answering the question :)Cretin
R
1

Django's architecture is designed to revolve around a Django project and several Django applications. 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.

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. 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.

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.

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.

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. With per project database settings, your already reusing code and keeping the database data and state distinct for each project at the same time.

Rand answered 9/6, 2012 at 15:15 Comment(1)
Forgive me for being dense, but I can't really see how this addresses the OP's questions… Could you clarify that a bit?Splayfoot

© 2022 - 2024 — McMap. All rights reserved.