我需要在列表显示上的 GenericForeignKey 字段上进行排序功能。
模型.py
class DealPayment(models.Model):
product_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
deal = GenericForeignKey('product_type', 'object_id')
# This deal can be of 'product' or 'certificate'
payment_date = models.DateTimeField(blank=True, null=True, help_text="Date when payment is done.")
transaction_amount = models.DecimalField(max_digits=16, decimal_places=2, default=0,
help_text="Total transaction amount for this item")
fees = models.DecimalField(max_digits=16, decimal_places=2, default=0,
help_text="Total fees for this item")
payment_amount = models.DecimalField(max_digits=16, decimal_places=2, default=0,
help_text="Total payment amount for this item to transfer to vendor")
# Some other fields
管理员.py
class DealPaymentAdmin(ImportExportModelAdmin):
readonly_fields=('product_type', 'payment_amount', 'fees', 'deal_status', 'deal', 'seller')
exclude = ('object_id', )
list_display = ('deal', 'seller', 'payment_amount', 'fees', 'rewards', 'fees_plan',
'payment_status', 'deal_status', 'closing_date', 'date_modified')
list_filter = ('payment_status',)
fields = ('product_type', 'deal', 'seller', 'deal_status', 'payment_amount', 'fees', 'payment_status','payment_date')
list_per_page = 30
def deal(self, obj):
return obj.product_type.model_class().objects.get(id=obj.object_id)
def deal_status(self, obj):
deal = obj.product_type.model_class().objects.get(id=obj.object_id)
if deal.publish_status and deal.active:
return "Active"
else:
return "Closed"
def closing_date(self, obj):
# Need to sort by this 'closing_date' field.
# Both type of deals having 'closing_date' field.
deal = obj.product_type.model_class().objects.get(id=obj.object_id)
return deal.closing_date
# Tried this.
closing_date.admin_order_field = 'deal__closing_date'
我从Django admin尝试了这个admin_order_field :how to sort by a custom list_display fields that has no database field but
但是我在尝试排序时得到了 FieldError 。我需要能够按管理列表显示中的close_date字段进行排序。
提前致谢。