Django管理员admin_order_field与其他表

时间:2016-09-07 16:41:35

标签: django admin

如何在Django admin中按自定义字段排序。 我的数据库表没有任何ForeignKey, django框架由相关船舶设计。

此模型定义:

class UserBaseInfo(BaseModel):
    STATUS = [(0, 'not pass'), (1, 'pass')]
    SEX = [(0, 'unset'), (1, 'male'), (2, 'female')]

    parent_id = models.IntegerField(max_length=11, default=0)
    level_id = models.IntegerField(max_length=11, default=1)
    phone = models.BigIntegerField(max_length=15, null=True, blank=True, default=None)
    nickname = models.CharField(max_length=100)
    sex = models.IntegerField(null=False, blank=False, default=1, choices=SEX)
    country = models.CharField(max_length=100, null=True, blank=True)
    province = models.CharField(max_length=100, null=True, blank=True)
    city = models.CharField(max_length=100, null=True, blank=True)
    headimgurl = models.CharField(max_length=255, null=True, blank=True)
    status = models.IntegerField(max_length=3, default=0)
    updated_time = models.BigIntegerField(max_length=18, null=True, blank=True)

    class Meta:
        db_table = 'user_base_info'
        ordering = ('-created_time', '-updated_time')

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        initial = False
        if not self.id:
            initial = True
        super(UserBaseInfo, self).save(force_insert=force_insert, force_update=force_update, using=using,
                                       update_fields=update_fields)
        if initial:
            Wallet(user_id=self.id, money=1000).save()


class Wallet(models.Model):
    user_id = models.BigIntegerField(max_length=18, primary_key=True, null=False, blank=False, verbose_name=_('UserId'))
    money = models.FloatField(null=False, blank=False)

    class Meta:
        db_table = 'wallet'
  

此管理员

class UserBaseInfoAdmin(CSVAdmin):
    list_display = ('nickname', 'avatar', 'level_id', 'parent', 'income', 'sex', 'country', 'province', 'city')
    list_filter = ('nickname', 'level_id', 'sex', ('created_time', DateFieldListFilter), ('updated_time', DateFieldListFilter))
    search_fields = ('nickname', 'level_id', 'sex', 'created_time', 'updated_time')
    list_display_links = ('level_id',)
    readonly_fields = ('nickname', 'level_id', 'sex', 'country', 'province', 'city', 'headimgurl',
                       'language', 'openid', 'privilege', 'created_time', 'updated_time')
    list_per_page = 20
    list_max_show_all = 20

    def avatar(self, data):
        return format_html('<img src="%s" height="50px" style="border-radius:50px"/>' % data.headimgurl)


    def parent(self, data):
        user = UserBaseInfo.objects.filter(pk=data.parent_id).first()
        return user.nickname if user else ''
    parent.allow_tags = True
    parent.admin_order_field = 'parent_id'

    def income(self, data):
        wallet = Wallet.objects.filter(user_id=data.id).first()
        if wallet:
            return wallet.money / 100
    income.admin_order_field = 'wallet__money'   
  

我想通过钱包订购,但我不知道下一步怎么做;

0 个答案:

没有答案