Django管理外键下拉与排序

时间:2016-11-22 11:37:35

标签: python django django-admin

我在django admin中有一个模型,如

class NotificationMapping(models.Model):
    counterId = models.ForeignKey(CounterDetails)
    groupId = models.ForeignKey(CounterGroup)


class Meta:
    unique_together = ('counterId', 'groupId',)

的ModelAdmin:

class NotificationMappingAdmin(admin.ModelAdmin):
    list_display = ('get_counterId', 'get_groupId',)
    actions = None

    # if display name is available for the counter display it
    # if not display counterId
    def get_counterId(self, obj):
        if obj.counterId.displayName:
            return obj.counterId.displayName
        else:
            return obj.counterId.counterId
    get_counterId.admin_order_field  = 'counterId'  # Allows column order sorting
    get_counterId.short_description = 'counter'  # Renames column head

    # show groupId on admin page for notification mapping
    def get_groupId(self, obj):
        return obj.groupId.groupId
    get_groupId.admin_order_field  = 'groupId'  # Allows column order sorting
    get_groupId.short_description = 'groupId'  # Renames column head

我需要在添加新条目的外键下拉列表中对值进行排序。

Counter Id dropdown image

1 个答案:

答案 0 :(得分:1)

你可以这样做:

--find-renames