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 8502065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:22:57+00:00 2026-06-11T01:22:57+00:00

we are running multiple django sites (let’s call them site1, site2, site3) against the

  • 0

we are running multiple django sites (let’s call them site1, site2, site3) against the same database, and we’d like to permit duplicated usernames accross them.
Site and auth framework do not seem to achieve this, by default, username is a unique field in auth.User.

So what I’ve done so far (monkey patch, messing up with the user object…):

User._meta.get_field('username')._unique = False
User.add_to_class('site', models.ForeignKey(Site, default=Site.objects.get_current().id, blank=True, null=True))
User._meta.unique_together = (('username', 'site'),)

This piece removes the uniqueness of username, add a site field, make the couple (username, site) unique.

Then come problems which could occure when requesting a User.objects.get(username=xx) (e.g., authentication backends), if some users have the same username on different site.
So, I decided to patch the User.objects manager:

def get_query_set(filter=True):
    q = QuerySet(User.objects.model, using=User.objects._db)
    if filter:
        return q.filter(site = Site.objects.get_current())
    return q
User.objects.get_query_set = get_query_set

Seems to work so far. But… the sites use pretty much the same objects, and it’s all likely we change user field of these objects using the admin interface, which is common to all sites… hence, if I want to attribute an object (which has a foreignkey to auh.User) to a user of site2 while being logged in as admin on site1, that won’t work, as the user manager will filter on site=site1.

I digged up a little, found that this seems to work:

class UserDefaultManager(UserManager):
    def get_query_set(self, filter=None):
        return QuerySet(User.objects.model)
User._default_manager = UserDefaultManager()

As far as I understand, _default_manager is used by the related objects manager.
Then, User.objects.get(username=xx) filter on sites, and an_object.user won’t.

Well, question is: yes, this is messy, and I’m pretty sure there will be flaws, but which are they ?

Next question is: if it’s valid, then where is the best place to put this piece of code ? It’s currently in a models.py file, just ran as the module is loaded…

  • 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-06-11T01:22:59+00:00Added an answer on June 11, 2026 at 1:22 am

    Instead of this I propose to use a profile :

    models.py:

    from django.contrib.auth.models import User
    
    
    class UserProfile(models.Model):
        """ Modèle ajoutant des propriété au modèle User """
        user = models.OneToOneField(User, editable=False)
        site1 = models.BooleanField()
        site2 = models.BooleanField()
        site3 = models.BooleanField()
    
    
    def create_user_profile(sender, instance, created, **kwargs):
        """ Crée la jonction entre le modèle User, et le modèle UserProfile """
        if created:
            UserProfile.objects.create(user=instance)
    
    post_save.connect(create_user_profile, sender=User)
    

    and on each site you create a decorator :

    decorators.py:

    try:
        from functools import wraps
    except ImportError:
        from django.utils.functional import wraps
    from django.http import HttpResponseForbidden
    from django.contrib.auth.decorators import login_required
    from distrib.views.error import error403
    
    
    def site1_required(function):
        @wraps(function)
        @login_required
        def decorateur(request, *k, **a):
            if request.user.get_profile().site1 or request.user.is_superuser:
                return function(request, *k, **a)
            else:
                result = error403(request)
                return HttpResponseForbidden(result)
        return decorateur
        return function
    

    then on each view you add the decorator, if the user is not allowed to connect on this site, he will get a http403 error.

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

Sidebar

Related Questions

I have multiple sites for the same client, on the same server, running django,
Suppose we have multiple sites (using sites-framework of Django) running on the same django
Is it possible to run multiple Django sites on the same server using Nginx
I currently have multiple Django sites running from one Apache server through WSGI and
We're looking into running multiple instances of Tomcat from the same copy of the
I am running multiple reports and combining them into a single PDF file. For
We are running multiple domains through the same code and we want to save
As the title says, I'm running multiple game servers, and every of them has
On an server running multiple ASP.NET sites, is it better to use one application
I'm trying to integrate a legacy database in Django. I'm running into problems with

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.