English 中文(简体)
恢复目前的项目状况(即最近关于Django 许多ToMany关系的日期)
原标题:Returning the current project status (i.e., most recent date on Django ManyToMany relationship)

Schema Description

A project s status can change over time. In order to track the status over time, I ve created a many-to-many relationship between the Project model and the ProjectStatusType model through the ProjectStatus intermediary table.

While this allows tracking a project s status over time, it increases the complexity of the schema such that retrieving the current status of a project or retrieving all open projects is more difficult.

“entergraph

Use Case

我希望能够归还所有处于特定状态的项目,例如所有<条码>>开放/代码>项目。 例如,用户到http://www.example.com/projects, 我只读到<代码>开放的项目,按违约情况在表格中展示。

Questions

  1. Should I denormalize the schema and add a current_status field in the Project model?
  2. If I shouldn t denormalize, what strategy should I use to retrieve the current status for each project? Should I create a property on the Project model that retrieves the current status?
最佳回答

如果你不需要搜寻,我将在项目模式上创造财产。 您可使用<代码>Max功能至aggregate ,以获得最新记录。

from django.db.models import Max

class Project(models.Model):
    [...]

    @property
    def status_date(self):
        return self.projectstatus_set.aggregate(newest=Max( status_date ))[ newest ]

该战略有文件

如果你需要做研究,那么你就应当去除,在<代码>上添加一个领域。 项目。 你们能够利用信号保持现状。 您希望添加一个<代码>的海报_save 倾听您的 ProjectStatus Field,这将把项目日期定在地位。

from django.db.signals import post_save

def update_status_date(sender, instance=None, **kwargs):
    project = instance.project
    project.status_date = max(project.status_date, instance.status_date)
    project.save()
post_save.connect(update_status_date, sender=ProjectStatus)

可在here上阅读更多信息。

================================================================================================================================================================================================================================================================

EDIT:自从撰写我的原始答复以来,OP组织在一定程度上澄清了他的问题,他的澄清改变了我两个战略的榜样,尽管这些战略不是基本建筑。 我想留给那些可能比我当时所回答的问题更需要的人原来的答案。

In my first example, he doesn t really want the newest status_date itself, but rather the newest project status type. This would change the property substantially; you don t need to use a MAX() SQL construct at all; you just want the first record attached to this object when ordered by date descending:

class Project(models.Model):
    [...]

    @property
    def project_status(self):
        return self.status.order_by( -status_date )[0]

与此相关的使用案例仍然相同。 如果你总是首先获得项目,然后想知道其现状,这是解决问题的正确途径。 如果您需要index按状况分列的项目,那么你就需要脱节。 这仍然是通过信号最好做到的,但不是像我在上述例子中那样节省时间,你可能想挽救一个描述。 但这一原则依然不变。

问题回答

您的描述表明,我假定你实际上使用<条码>项目Status作为<条码>,通过<><>>>>> >> ;你已经重新储存了与该模式的关系方面的额外数据。 如果额外数据的项目之一已经不是确定该特定状况的日期,我还要补充一下。

届时,您可下令将项目代码<>项目代码>(>>)上报,因此,首批<项目代码>(<>>项目的回归将始终是最新的(目前)。





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签