Django过滤最新的相关对象 [英] Django Filter by latest related object

查看:167
本文介绍了Django过滤最新的相关对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个系统来跟踪数据库中各种对象的审核任务。这些通过通用外键关系链接。给定一个ObjectModerationState对象,我必须从最新的StateTransisition对象中确定其状态。

I've setup a system to track moderation tasks for various objects in the database. These are linked via a Generic Foreign Key relation. Given a ObjectModerationState Object I must determine its state from the most recent StateTransisition object.

解决方案必须借助于SimpleListFilter自己才能使用list_filter对list_display进行排序。

The solution must lend it self to be used in a SimpleListFilter for sorting on list_display using list_filter.

例如给定状态
- 排队
- 已完成
- 验证

e.g. Given States - Queued - Completed - Verify

我应该可以根据最新状态值过滤ObjectModerationState对象在StateTransption对象中。

I should be able to filter ObjectModerationState Objects, based on the latest state value in the StateTransisition object.

我一直倾向于ObjectModerationState上的某种注释。

I've been leaning towards some sort of annotation on the ObjectModerationState.

这是我没有工作的。

class ObjectModerationState(models.Model):
    job = models.ForeignKey(JobDescription)
    object_id = models.TextField(help_text="Primary key of the model under moderation.")
    content_type = models.ForeignKey(ContentType, help_text="Content type of the model under moderation.")
    object = generic.GenericForeignKey()

class StateTransisition(models.Model):
    state = models.ForeignKey(State)
    moderation_details = models.ForeignKey("ObjectModerationState", related_name='states')
    changed_by = models.ForeignKey("auth.User", related_name='+', null=False,
                                   help_text="If you create the task, then usually this will be you.")
    date_updated = models.DateTimeField(_("Date Set"))
    last_modified = models.DateTimeField(auto_now=True)


    def __unicode__(self):
        return "%s in state %s by %s" % (self.moderation_details.object, self.state, self.changed_by)

    class Meta:
        ordering = ['last_modified', ]
        get_latest_by = 'last_modified'

class State(Orderable):
    label = models.CharField(_("State Label"), max_length=100, unique=True)
    description = models.TextField(_("Description"), max_length=200, null=True, blank=True,
                                   help_text=_("Explination of this state in context"))
    valid_transitions = models.ManyToManyField("State", null=True, blank=True,
                                               help_text=_("You must add these mappings."))

    def __unicode__(self):
        return "%s" % (self.label)

    class Meta:
        abstract = False



admin.py



admin.py

class CurrentStateFilter(SimpleListFilter):
    title = 'current state'
    parameter_name = 'current state'

    def lookups(self, request, model_admin):
        states = State.objects.all()
        return  [(c.id, c) for c in states]

    def queryset(self, request, queryset):
        if self.value():
            queryset.filter(states__state__id=self.value()) <<< ????
        else:
            return queryset



编辑




  • 如果解决方案是正确的,我相信这个问题类似于这似乎是无法做到的。 django查询集上的复杂注释

  • EDIT

    • I believe the problem is similar to this which it seems can't be done, if the solution is correct. complex annotate on django query set
    • 推荐答案

      经过多次研究,我不确定它可以做到没有非常黑客。我最终直接在list_display中显示的模型上缓存值。我使用后保存信号。它工作得很好。

      After much research I'm not sure it can be done without being very hacky. I ended up caching the values directly on the model being displayed in the list_display. I did this using post save signals. It works pretty well.

      这篇关于Django过滤最新的相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆