gpt4 book ai didi

python - Django Admin list_display 产品列表

转载 作者:行者123 更新时间:2023-12-04 02:06:18 24 4
gpt4 key购买 nike

我是 Django 新手,我尝试修改一个项目来学习。

我在一个模型中有两个类('Order' 和 'OrderItem'),OrderItem 类存储客户订单中选择的所有项目。

models.py


class Order(models.Model):

STATUS_CHOICES = (
(0, 'Waiting Payment'),
(1, 'Completed'),
(2, 'Canceled'),
)

PAYMENT_OPTION_CHOICES = (
('deposit', 'deposit'),
('paypal', 'Paypal'),
)

user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='User')
status = models.IntegerField(
'Situation', choices=STATUS_CHOICES, default=0, blank=True
)
payment_option = models.CharField(
'Payment Options', choices=PAYMENT_OPTION_CHOICES, max_length=20,
default='deposit'
)

created = models.DateTimeField('Created in', auto_now_add=True)
modified = models.DateTimeField('Modified in', auto_now=True)

objects = OrderManager()

class Meta:
verbose_name = 'Order'
ordering = ('-created',)

def __str__(self):
return 'Order #{}'.format(self.pk)

def products(self):
products_ids = self.items.values_list('product')
return Product.objects.filter(pk__in=products_ids)

def total(self):
aggregate_queryset = self.items.aggregate(
total = models.Sum(
models.F('price') * models.F('quantity'),
output_field = models.DecimalField()
)
)
return aggregate_queryset['total']



class OrderItem(models.Model):

order = models.ForeignKey(Order, verbose_name='Order', related_name='items')
product = models.ForeignKey('event.Product', verbose_name='Product')
quantity = models.PositiveIntegerField('Quantity', default=1)
price = models.DecimalField('Price', decimal_places=2, max_digits=8)

class Meta:
verbose_name = 'Order Item'

def __str__(self):
return '{}'.format(self.product)

在 django admin 中,我可以显示所有订单,当我点击查看更多时,我会看到此订单上的所有产品,但我的问题是,我无法在我的 list_display 中列出 OrderItem 类的这个产品,我该怎么做?

admin.py


class OrderItemInline(admin.StackedInline):
model = OrderItem
fields = ['product']
readonly_fields = ['product',]
extra = 0
max_num = 0


class OrderAdmin(admin.ModelAdmin):

model = Order
inlines = [ OrderItemInline, ]

readonly_fieldsets = (
(None, {
'fields': ('user','status','order','created')
}),
)
readonly_fields = ['user','status','payment_option']

search_fields = ['user__name', 'user__email']
list_filter = ['status', ]

list_display = ['pk','user','status','created','product']
ordering = ('-created',)


admin.site.register(Order, OrderAdmin)

最佳答案

这是一个容易的任务。在您的 OrderAdmin 内类,删除 product来自 display_list列出并添加 string that will be the name of a method/callable ,说 list_products .现在,list_display将显示该函数的返回值。

例如,定义一个 list_products OrderAdmin里面的方法类(class)。

from django.utils.html import mark_safe


class OrderAdmin(admin.ModelAdmin):

list_display = ['pk', 'user', 'status', 'created', 'list_products']

def list_products(self, obj):
# each obj will be an Order obj/instance/row
to_return = '<ul>'
# I'm assuming that there is a name field under the event.Product model. If not change accordingly.
to_return += '\n'.join('<li>{}</li>'.format(pro_name) for prod_name in obj.items.values_list('product__name', flat=True))
to_return += '</ul>'
return mark_safe(to_return)

关于python - Django Admin list_display 产品列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43132069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com