我有以下型号:
class Article(models.Model):
fabric = models.CharField(max_length = 10)
color = models.CharField(max_length = 10)
supplier = models.CharField(max_length = 50)
def __str__(self):
return str(self.id)
class Import(models.Model):
packing_list = models.ForeignKey(PackingList)
article = models.ForeignKey(Article)
quantity = models.IntegerField()
comment = models.CharField(max_length = 200)
def __str__(self):
return str(self.id)
在admin.py
我有以下代码:
class ImportAdmin(admin.ModelAdmin):
def fabric(self,obj):
return obj.article.fabric
def color(self,obj):
return obj.article.color
def supplier(self,obj):
return obj.article.supplier
list_display = ['id', 'fabric', 'color', 'supplier','quantity','comment']
search_fields = ['packing_list__name']
在我的管理员面板中,我想按fabric
对字段进行排序。
我尝试ordering = ('fabric', )
,但我无法这样做,因为fabric
不是Import
的属性。
有没有办法做到这一点?