Python 列表中的多对多显示 django

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18108521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:55:39  来源:igfitidea点击:

many-to-many in list display django

pythondjangodjango-adminadmindjango-queryset

提问by Mdjon26

class PurchaseOrder(models.Model):
    product = models.ManyToManyField('Product')
    vendor = models.ForeignKey('VendorProfile')
    dollar_amount = models.FloatField(verbose_name='Price')


class Product(models.Model):
   products = models.CharField(max_length=256)

   def __unicode__(self):
       return self.products

I have that code. Unfortunately, the error comes in admin.py with the ManyToManyField

我有那个代码。不幸的是,错误出现在 admin.py 中ManyToManyField

class PurchaseOrderAdmin(admin.ModelAdmin):
    fields = ['product', 'dollar_amount']
    list_display = ('product', 'vendor')

The error says:

错误说:

'PurchaseOrderAdmin.list_display[0]', 'product' is a ManyToManyField which is not supported.

'PurchaseOrderAdmin.list_display[0]', 'product' 是不受支持的多对多字段。

However, it compiles when I take 'product'out of list_display. So how can I display 'product'in list_displaywithout giving it errors?

但是,当我'product'取出list_display. 那么我怎样才能'product'list_display不出错的情况下显示呢?

edit: Maybe a better question would be how do you display a ManyToManyFieldin list_display?

编辑:也许更好的问题是你如何显示ManyToManyFieldin list_display

采纳答案by karthikr

You may not be able to do it directly. From the documentation of list_display

您可能无法直接执行此操作。从文档list_display

ManyToManyField fields aren't supported, because that would entail executing a separate SQL statement for each row in the table. If you want to do this nonetheless, give your model a custom method, and add that method's name to list_display. (See below for more on custom methods in list_display.)

不支持 ManyToManyField 字段,因为这需要为表中的每一行执行单独的 SQL 语句。尽管如此,如果您想这样做,请为您的模型提供一个自定义方法,并将该方法的名称添加到 list_display。(有关 list_display 中自定义方法的更多信息,请参见下文。)

You can do something like this:

你可以这样做:

class PurchaseOrderAdmin(admin.ModelAdmin):
    fields = ['product', 'dollar_amount']
    list_display = ('get_products', 'vendor')

    def get_products(self, obj):
        return "\n".join([p.products for p in obj.product.all()])

OR define a model method, and use that

或者定义一个模型方法,并使用它

class PurchaseOrder(models.Model):
    product = models.ManyToManyField('Product')
    vendor = models.ForeignKey('VendorProfile')
    dollar_amount = models.FloatField(verbose_name='Price')

    def get_products(self):
        return "\n".join([p.products for p in self.product.all()])

and in the admin list_display

并在管理员中 list_display

list_display = ('get_products', 'vendor')

回答by JKV

This way you can do it, kindly checkout the following snippet:

这样你就可以做到这一点,请查看以下代码段:

class Categories(models.Model):
    """ Base category model class """

    title       = models.CharField(max_length=100)
    description = models.TextField()
    parent      = models.ManyToManyField('self', default=None, blank=True)
    when        = models.DateTimeField('date created', auto_now_add=True)

    def get_parents(self):
        return ",".join([str(p) for p in self.parent.all()])

    def __unicode__(self):
        return "{0}".format(self.title)

And in your admin.py module call method as follows:

在您的 admin.py 模块调用方法中,如下所示:

class categories(admin.ModelAdmin):
    list_display    = ('title', 'get_parents', 'when')