I have a simple store / product relationship and I want to sort the products depending on the store name alphabetically.
models:
class Store(models.Model):
name = models.CharField("name", max_length = 128)
show_name = models.CharField("name", max_length = 128, null = True, blank = True)
class Product(models.Model):
number = models.PositiveIntegerField()
name = models.CharField("name", max_length = 128)
store = models.ForeignKey(Store, on_delete = models.CASCADE)
and in the admin:
class ProductAdmin(admin.ModelAdmin):
list_display = ["number", "name", "get_store_name",]
def get_store_name(self, obj):
if obj.store.show_name == None:
return f"""{obj.store.name}"""
else:
return f"""{obj.store.show_name}"""
I know I cannot use order_by
on custom fields. So I thought I need to override the get_queryset
method probably? I found multiple examples to annotate by counted or calculated numbers, but never for strings.