Is Django sites framework suitable for serving sites with a different logic and models structure?

288 Views Asked by At

I am working on a application, which was written by other people. It actively uses the sites framework to serve two different gaming sites. These sites run on the same code, on the same database:

class Entry(models.Model):
    headline = models.CharField(max_length=100)
    body = models.TextField()
    created = models.DateTimeField()
    creator = models.ForeignKey(User)

    # Site1 fields
    best = models.BooleanField()
    is_editor_post = models.BooleanField()
    site1_views = models.IntegerField()
    cnt_comments = models.IntegerField()
    last_edit = models.DateTimeField()
    editor = models.ForeignKey(User)
    tags = models.ManyToManyField(Tag)

    # Site2 fields
    category = models.ForeignKey(Category)
    blog = models.ForeignKey(Blog)
    site2_views = models.IntegerField()

class Game(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    genre = models.ForeignKey(Genre)
    release_date = models.DateField()
    platform = models.ForeignKey(Platform)

    # Site1 fields
    mark = models.IntegerField()
    badges = models.ManyToManyField(Badge)
    badges_updated = models.DateTimeField()

    # Site2 fields
    requirements = models.CharField(max_length=255)
    status = models.IntegerField()
    has_news = models.BooleanField()
    has_articles = models.BooleanField()
    has_screenshots = models.BooleanField()

    class Meta:
       permissions = (
           ('site1_add_game', 'Site1/Can add game'),
           ('site2_add_game', 'Site2/Can add game'),
       )

As you can see, there is a bunch of fields, useless for one of the sites. Also, we don't use default permissions, but instead create our own. This is because the users a shared between the two sites, and one use could have right to add games on both sites, while another person is only able to add it on site2.

There is also a lot of code in views, model and manager methods like this:

def get_related_entries(self):
    if settings.SITE_ID == 1:
       # code for finding related entries on site1
    elif settings.X_SITE_ID == 2:
       # code for finding related entries on site2
    return entries

So when I am working on site1 code, the fields and code from site2 is just confusing me. If I add a feature to the first site, most often I don't need such a feature on another. Also I will probably have to write a third site, and they want this site to be running on the same code and database. Why? Because they think this enables code reusing and sharing content between different sites. This sounds really good, and there is really a bunch of code, which is common beetween the sites. But there is also very much differences in the sites logic and models structure to run them on the same code.

The code is very hard to maintain, and it just looks ugly and not straightforward. The solution might be to separate common apps, and site-specific apps. But almost all apps have site-specific code. I think it would be much simpler if I had two different projects, each one with its own code and models.

So, is the sites framework is suitable here? When is it suitable? Why does Django "strongly encourages" it's using?

1

There are 1 best solutions below

1
On BEST ANSWER

Even though i haven't much experience with the sites framework, i think it mostly makes sense when you want to use the same admin interface for all sites. And that's a requirement you haven't even mentioned.

Recently i considered using it, but after some inital coding i decided against it. Things got complicated very quick. If statements everywhere, extra fields that i basically needed only on one site, permission handling looked tricky, pretty much all what you have mentioned.

I'm sure there are use cases for the sites framework, but when in doubt i would go for separate projects with reusable apps where the models can be subclassed when needed.