データベース内のさまざまなオブジェクトのモデレーション タスクを追跡するシステムをセットアップしました。これらは、汎用外部キー関係を介してリンクされています。ObjectModerationState オブジェクトを指定すると、最新の StateTransisition オブジェクトからその状態を判断する必要があります。
このソリューションは、list_filter を使用して list_display で並べ替えるために、SimpleListFilter で使用できるようにする必要があります。
例: 与えられた状態 - キューに入れられた - 完了 - 検証
StateTransisition オブジェクトの最新の状態値に基づいて、ObjectModerationState オブジェクトをフィルタリングできるはずです。
私は、ObjectModerationState に関するある種の注釈に傾倒してきました。
これが私が持っているもので、機能していません。
models.py:
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
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クエリセットの複雑な注釈