Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3676094
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:03:29+00:00 2026-05-19T03:03:29+00:00

I’ve writing a Django site that uses two different databases. One is the local,

  • 0

I’ve writing a Django site that uses two different databases. One is the local, let’s call it, “Django”, database that stores all of the standard tables from a pretty standard install — auth, sites, comments, etc. — plus a few extra tables.

Most of the data, including users, comes from a database on another server, let’s call it the “Legacy” database.

I’m looking for suggestions on clean, pythonic ways of connecting the two databases, particularly in regards to users.

I’m using a proxy model, which works great when I can explicitly use it, but I run into problems when I’m accessing the user object as a related object (for example, when using the built-in django comments system).

Here’s what the code looks like:

models.py: (points to the Django database)

from django.db import models
from django.conf import settings
from django.contrib.auth.models import User as AuthUser, UserManager as AuthUserManager, AnonymousUser as AuthAnonymousUser

class UserPerson(models.Model):
    user = models.OneToOneField(AuthUser, related_name="person")
    person_id = models.PositiveIntegerField(verbose_name='Legacy ID')

    def __unicode__(self):
        return "%s" % self.get_person()

    def get_person(self):
        if not hasattr(self, '_person'):
            from legacy_models import Person
            from utils import get_person_model
            Person = get_person_model() or Person
            self._person = Person.objects.get(pk=self.person_id)
        return self._person
    person=property(get_person)

class UserManager(AuthUserManager):
    def get_for_id(self, id):
        return self.get(person__person_id=id)

    def get_for_email(self, email):
        try:
            person = Person.objects.get(email=email)
            return self.get_for_id(person.pk)
        except Person.DoesNotExist:
            return User.DoesNotExist

    def create_user(self, username, email, password=None, *args, **kwargs):
        user = super(UserManager,self).create_user(username, email, password, *args, **kwargs)
        try:
            person_id = Person.objects.get(email=email).pk
            userperson, created = UserPerson.objects.get_or_create(user=user, person_id=person_id)
        except Person.DoesNotExist:
            pass
        return user

class AnonymousUser(AuthAnonymousUser):
    class Meta:
        proxy = True

class User(AuthUser):
    class Meta:
        proxy=True

    def get_profile(self):  
        """
        Returns the Person record from the legacy database
        """
        if not hasattr(self, '_profile_cache'):
            self._profile_cache = UserPerson.objects.get(user=self).person
        return self._profile_cache

    objects = UserManager()

legacy_models.py: (points to the “Legacy” database)

class Person(models.Model):
    id = models.AutoField(primary_key=True, db_column='PeopleID') # Field name made lowercase.
    code = models.CharField(max_length=40, blank=True, db_column="person_code", unique=True)
    first_name = models.CharField(max_length=50, db_column='firstName', blank=True) # Field name made lowercase.
    last_name = models.CharField(max_length=50, db_column='lastName', blank=True) # Field name made lowercase.
    email = models.CharField(max_length=255, blank=True)

    def __unicode__(self):
        return "%s %s" % (self.first_name, self.last_name)

    def get_user(self):
        from models import User
        if not hasattr(self,'_user'):
            self._user = User.objects.get_for_id(self.pk)
        return self._user
    user = property(get_user)

    class Meta:
        db_table = u'People'

I’ve also whipped up my own middleware, so request.user is the proxy User object also.

The real problem is when I’m using something that has user as a related object, particularly in a template where I have even less control.

In the template:

{{ request.user.get_profile }} 
{# this works and returns the related Person object for the user #}

{% for comment in comments %} {# retrieved using the built-in comments app %}
    {{ comment.user.get_profile }}
    {# this throws an error because AUTH_PROFILE_MODULE is not defined by design #}
{% endfor %}

Short of creating a wrapped version of the comments system which uses my proxy User model instead, is there anything else I can do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:03:30+00:00Added an answer on May 19, 2026 at 3:03 am

    Here’s how I resolved it. I stopped using the User proxy altogether.

    models.py:

    from django.db import models
    from legacy_models import Person
    from django.contrib.auth.models import User
    
    class UserPerson(models.Model):
        user = models.OneToOneField(User, related_name="person")
        person_id = models.PositiveIntegerField(verbose_name='PeopleID', help_text='ID in the Legacy Login system.')
    
        def __unicode__(self):
            return "%s" % self.get_person()
    
        def get_person(self):
            if not hasattr(self, '_person'):
                self._person = Person.objects.get(pk=self.person_id)
            return self._person
        person=property(get_person)
    
    class LegacyPersonQuerySet(models.query.QuerySet):
        def get(self, *args, **kwargs):
            person_id = UserPerson.objects.get(*args, **kwargs).person_id
            return LegacyPerson.objects.get(pk=person_id)
    
    class LegacyPersonManager(models.Manager):
        def get_query_set(self, *args, **kwargs):
            return LegacyPersonQuerySet(*args, **kwargs)
    
    class LegacyPerson(Person):
        objects = LegacyPersonManager()
    
        class Meta:
            proxy=True
    

    and legacy_models.py:

    class Person(models.Model):
        id = models.AutoField(primary_key=True, db_column='PeopleID') # Field name made lowercase.
        code = models.CharField(max_length=40, blank=True, db_column="person_code", unique=True)
        first_name = models.CharField(max_length=50, db_column='firstName', blank=True) # Field name made lowercase.
        last_name = models.CharField(max_length=50, db_column='lastName', blank=True) # Field name made lowercase.
        email = models.CharField(max_length=255, blank=True)
    
        def __unicode__(self):
            return "%s %s" % (self.first_name, self.last_name)
    
        def get_user(self):
            from models import User
            if not hasattr(self,'_user'):
                self._user = User.objects.get_for_id(self.pk)
            return self._user
        def set_user(self, user=None):
            self._user=user
        user = property(get_user, set_user)
    
        class Meta:
            db_table = u'People'
    

    Finally, in settings.py:

    AUTH_PROFILE_MODULE = 'myauth.LegacyPerson'
    

    This is a simpler solution, but at least it works! It does mean that whenever I want the legacy record I have to call user_profile, and it means that there’s an additional query for each user record, but this is a fair trade-off because actually it isn’t very likely that I will be doing a cross check that often.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm making a simple page using Google Maps API 3. My first. One marker
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.