Sites framework or custom model for multi-sites

I want to launch a Django 4 site with multiple domains linked to the same project.
I created a model like this :

class Sites(models.Model):    
    domain = models.CharField(max_length=100)
    name = models.CharField(max_length=100)
    google_analytics_code = models.CharField(max_length=25)
    email = models.EmailField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

    def __str__(self):
        return self.name

Then I came across the Sites framework in Django.

  1. I think the Sites framework will do instead of creating a class Sites(models.Model) right ?
  2. How do I enter a list of 20 domain names with some meta data like name etc to the project using the built-in Sites framework ?

When you say:

Are you saying?

  • You want one project, with one set of models, effectively serving the same content for multiple different domains

or

  • You want one project, with some data and content the same, serving some identical content and some site-specific content.

or

  • You want one installation of code, serving totally unique content for each domain.

My understanding of the sites framework is that it’s targeted at the second case. If I were faced with either the first or third case, I’d probably do something other than that.

You want one project, with one set of models, effectively serving the same content for multiple different domains - No.

You want one installation of code, serving totally unique content for each domain - This is what I thought first but then I realized there may be some or very few overlapping content in more than one domain (site).

So basically #2. If I include from django.contrib.sites.models import Site will I be able to enter the data in the admin ? And how do I enter some extra site fields / columns ?

https://docs.djangoproject.com/en/4.0/ref/contrib/sites/#associating-content-with-multiple-sites

Yes. The module includes an admin.py file for adding Site model data.

Create a model (perhaps named something like “SiteExtension”) with a One-to-One relationship to Site. The Sites framework won’t do anything with it, but your project will have full access to it.
And, if you register SiteExtension in the admin, the “Add” function for SiteExtension will show you the input fields for both models, so it’ll let you create it all at once.

But when I create a separate class extending Site & registering only Site_Custom, the dashboard is showing 2 different tables.

models.py :

class Site_Custom(Site):
    google_analytics_code = models.CharField(max_length=25)
    email = models.EmailField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

    def __str__(self):
        return self.name

admin.py :

from .models import Site_Custom
admin.site.register([Site_Custom])

I want the default Site to be extended and shown as primary.

I guess the solution is to create a new table with one-to-one link, but I was looking for single table with additional fields.

from django.contrib.sites.models import Site

class Site_Custom(models.Model):
    site = models.OneToOneField(Site, on_delete=models.CASCADE)
    google_analytics_code = models.CharField(max_length=25)
    email = models.EmailField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)    
    active = models.BooleanField(default=True)

I’m not sure what the link you’re referencing has to do with this topic - it’s a completely different situation and model structure.

But sure, you have a choice. Create your own fork of Django and extend the django.contrib.sites.models package as you see fit. I just never recommend that as an option because long-term, it creates more problems than it solves.

1 Like