我是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管理员中我可以显示所有订单,当我点击查看更多我看到此订单上的所有产品,但问题是,我无法在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)
答案 0 :(得分:3)
这是一件容易的事。在OrderAdmin
课程内,从product
列表中删除display_list
,然后添加string that will be the name of a method/callable,说list_products
。现在,list_display
将显示该函数的返回值。
例如,在list_products
类中定义OrderAdmin
方法。
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)
答案 1 :(得分:0)
这是一个简单的任务。在您的OrderAdmin类中,从display_list列表中删除产品,并添加一个字符串,该字符串将作为方法/可调用的名称,例如list_products。现在,list_display将显示该函数的返回值。
例如,在OrderAdmin类中定义一个list_products方法:
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)
答案 2 :(得分:-1)
我明白了,我不知道这是不是真的。
我先创建一个列表。
def list_products(self, obj):
products = []
for prod_name in obj.items.values_list('product__name', flat=True):
products.append(''.join(prod_name))
to_return = '<br/>'.join(products)
return mark_safe(to_return)