我有以下models.py我想知道有多少訂單,每個客戶的放置
from django.db import models
class Customer(models.Model):
Person_Id=models.IntegerField()
Person_Name=models.CharField(max_length=100)
Address=models.CharField(max_length=500)
Email=models.EmailField(null=False, default="")
def __str__(self):
return self.Person_Name
class Items(models.Model):
Item_Code=models.IntegerField()
Item_Name=models.CharField(max_length=200)
def __str__(self):
return self.Item_Name
class Orders(models.Model):
Order_Id=models.ForeignKey(Customer, on_delete=models.CASCADE)
Item_Name=models.ManyToManyField(Items)
Order_date=models.DateField()
Comment=models.CharField(max_length=1000, blank=True)
def __str__(self):
return str(self.Order_Id)
admin.py是遵循
from django.contrib import admin
from .models import Customer, Orders, Items
class CustomerDetails(admin.ModelAdmin):
list_display = ('Person_Name', 'Address', 'Email','total','count')
readonly_fields = ('total',)
search_fields = ('Person_Name', 'Email')
list_filter = ('Address',)
ordering = ('-Person_Name',)
class OrderDetails(admin.ModelAdmin):
filter_horizontal = ('Item_Name',)
admin.site.register(Customer, CustomerDetails)
admin.site.register(Items)
admin.site.register(Orders, OrderDetails)
現在,我想知道每個客戶有多少訂單。我知道反向查找外鍵會被使用,但我沒有得到正確的方法。
你的意思是你想要的順序計數在管理列表顯示中顯示? –
@DanielRoseman是的,我想在管理列表顯示中的訂單數。 –