Django 站点框架是否适合为具有不同逻辑和模型结构的站点提供服务?

发布于 2024-10-20 00:49:05 字数 2357 浏览 1 评论 0原文

我正在开发一个由其他人编写的应用程序。它积极使用站点框架来为两个不同的游戏站点提供服务。这些站点在相同的代码、相同的数据库上运行:

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'),
       )

如您所见,有一堆字段,对于其中一个站点来说毫无用处。此外,我们不使用默认权限,而是创建我们自己的权限。这是因为用户在两个站点之间共享,一个用户可以有权在两个站点上添加游戏,而另一个用户只能在 site2 上添加游戏。

视图、模型和管理器方法中也有很多代码,如下所示:

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

因此,当我处理 site1 代码时,site2 中的字段和代码让我感到困惑。 如果我向第一个站点添加一项功能,大多数情况下我不需要在另一个站点上使用此类功能。另外,我可能需要编写第三个站点,他们希望该站点在相同的代码和数据库上运行。为什么?因为他们认为这可以实现代码重用和在不同站点之间共享内容。这听起来确实不错,而且确实有一堆代码,这些代码在网站之间很常见。但在相同代码上运行它们的站点逻辑和模型结构也存在很大差异。

代码很难维护,而且看起来很丑而且不简单。解决方案可能是将通用应用程序和特定于站点的应用程序分开。但几乎所有应用程序都有特定于站点的代码。我认为如果我有两个不同的项目,每个项目都有自己的代码和模型,事情会简单得多。

那么,网站框架适合这里吗?什么时候合适?为什么 Django " 强烈鼓励“它正在使用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2024-10-27 00:49:05

尽管我对网站框架没有太多经验,但我认为当您想对所有网站使用相同的管理界面时,它最有意义。这是您甚至没有提到的要求。

最近我考虑使用它,但经过一些初始编码后我决定不使用它。事情很快就变得复杂了。如果语句无处不在,我基本上只在一个站点上需要的额外字段,权限处理看起来很棘手,几乎都是您提到的。

我确信网站框架有用例,但是当有疑问时,我会选择具有可重用应用程序的单独项目,其中模型可以在需要时进行子类化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文