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!
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! – Cretinrelated_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. – Cretintags = TaggableManager(blank=True) tags.rel.related_name = "appA"
- that does solve the problem of thetags
variable clashing, but unfortunately not thetagged_items
variable; I still get the same errors & I can't figure out how to passrelated_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. – GerminUserProfile
andTaggedItem
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 thisFOO_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