英文:
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
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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论