class MyModel(models.Model):
...
@property
def admin_last_modified(self):
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.models import ContentType
try:
logentry = LogEntry.objects.filter(
object_id=self.pk,
content_type_id=ContentType.objects.get_for_model(self.__class__)
).order_by('action_time')[0]
except:
return ''
return '{} by {}'.format(logentry.action_time, logentry.user)
管理类:
@admin.register(MyModel)
class MyAdmin(admin.ModelAdmin):
list_display = ( ..., 'admin_last_modified' )
为此,根据我收集到的信息,我需要将这个属性重新表述为某种注释。但我试过却没有想出一个办法:
class MyAdmin(admin.ModelAdmin):
def get_queryset(self):
queryset = super().get_queryset(request)
queryset = queryset.annotate(
admin_last_modified=# ???
)
return queryset
我现在在找什么来代替问号。