如何使用admin.display装饰器按两个列排序结果

huangapple go评论69阅读模式
英文:

How to order the results by two columns using admin.display decorator

问题

You can specify the order using the admin.display() decorator like this:

# admin.py
@admin.register(Case)
class CaseAdmin(admin.ModelAdmin):
    list_display = ['case_number', ...]

    @admin.display(ordering=['-year', '-number'])
    def case_number(self, case):
        return f'{case.number} / {case.year}'

This code will order the list display of cases by year and then by number in descending order.

英文:

Please consider the following example:

# models.py

class Case(models.Model):
    number = models.SmallIntegerField(null=False, blank=False)
    year = models.SmallIntegerField(null=False, blank=False)
    ...
#admin.py
@admin.register(Case)
class CaseAdmin(admin.ModelAdmin):
    list_display = ['case_number', ...]
    ordering = ['-year', '-number']

    @admin.display(ordering=['-year', '-number'])
    def case_number(self, case):
        return f'{case.number} / {case.year}'

Now how can I specify the order to be by '-year' and '-number' using the admin.display() decorator?

If it can not be done with the admin.display() decorator, is there another way to achieve that?

Thanks

答案1

得分: 0

这是您要翻译的内容:

"it's possible but it's a very slow link here"

"Django order_by() query method order records using model field"

"Here I added an alternative way for it. using save() method overriding please check here..."

models.py

class Case(models.Model):
    number = models.SmallIntegerField(null=False, blank=False)
    year = models.SmallIntegerField(null=False, blank=False)
    case_number = models.PositiveIntegerField(null=True, blank=True)
    
    def save(self, *args, **kwargs):
        self.case_number = int(f"{self.number}{self.year}")
        super(Case, self).save(*args, **kwargs)

admin.py

@admin.register(Case)
class CaseAdmin(admin.ModelAdmin):
    list_display = ("case_number", "year", "number", "id")[::-1]
    ordering = ['-case_number']

Browser Output

如何使用admin.display装饰器按两个列排序结果

NOTE - here you need to add one extra field for saving case_number but, it's a better way to order records using case_number

英文:

it's possible but it's a very slow link here

Django order_by() query method order records using model field

Here I added an alternative way for it. using save() method overriding please check here...

models.py

class Case(models.Model):
    number = models.SmallIntegerField(null=False, blank=False)
    year = models.SmallIntegerField(null=False, blank=False)
    case_number = models.PositiveIntegerField(null=True, blank=True)
    
    def save(self, *args, **kwargs):
        self.case_number = int(f"{self.number}{self.year}")
        super(Case, self).save(*args, **kwargs)

admin.py

@admin.register(Case)
class CaseAdmin(admin.ModelAdmin):
    list_display = ("case_number", "year", "number", "id")[::-1]
    ordering = ['-case_number']

Browser Output

如何使用admin.display装饰器按两个列排序结果

NOTE - here you need to add one extra field for saving case_number but, it's a better way to order records using case_number

答案2

得分: 0

感谢 @Willem Van Onsem 为提供的 patch,使这成为可能。

尽管开发人员没有同意实施它,但还是感谢您花时间修补并打开了 #34646 问题。

英文:

Thank you @Willem Van Onsem for the patch you provided in order to make this possible.

Even thought the developers didn't agree to implement it. Thank you for the time you spent patching it and opening the #34646 ticket.

huangapple
  • 本文由 发表于 2023年6月8日 01:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定