我无法过滤订单的产品供应商
I can't filter the product vendor for orders
我希望特殊用户(供应商)在他们的 django 管理中看到他们自己的订单。每当我尝试过滤掉订购产品的用户时。这是代码。
我在我的产品模型中连接了供应商。我不知道如何将供应商与他们各自的订单联系起来。
class Order(models.Model):
STATUS = (
('New', 'New'),
('Accepted', 'Accepted'),
('Preparing', 'Preparing'),
('OnShipping', 'OnShipping'),
('Completed', 'Completed'),
('Canceled', 'Canceled'),
)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
code = models.CharField(max_length=5, editable=False)
first_name = models.CharField(max_length=10)
last_name = models.CharField(max_length=10)
phone = models.CharField(blank=True, max_length=20)
address = models.CharField(blank=True, max_length=150)
city = models.CharField(blank=True, max_length=20)
country = models.CharField(blank=True, max_length=20)
total = models.FloatField()
status=models.CharField(max_length=10,choices=STATUS,default='New')
ip = models.CharField(blank=True, max_length=20)
adminnote = models.CharField(blank=True, max_length=100)
create_at=models.DateTimeField(auto_now_add=True)
update_at=models.DateTimeField(auto_now=True)
class OrderProduct(models.Model):
STATUS = (
('New', 'New'),
('Accepted', 'Accepted'),
('Canceled', 'Canceled'),
)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
variant = models.ForeignKey(Variants, on_delete=models.SET_NULL,blank=True, null=True) # relation with variant
quantity = models.IntegerField()
price = models.FloatField()
amount = models.FloatField()
status = models.CharField(max_length=10, choices=STATUS, default='New')
create_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.product.title
class OrderAdmin(admin.ModelAdmin):
list_display = ['first_name', 'last_name','phone','city','total','status','create_at']
list_filter = ['status']
readonly_fields = ('user','address','city','country','phone','first_name','ip', 'last_name','phone','city','total')
can_delete = False
inlines = [OrderProductline]
class OrderProductAdmin(admin.ModelAdmin):
list_display = ['user', 'product','price','quantity','amount']
list_filter = ['product']
def get_queryset(self, request):
qs = super().get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(product=request.product)```
更改您的 ModelAdmin
子类:
class OrderProductAdmin(admin.ModelAdmin):
list_display = ['user', 'product','price','quantity','amount']
list_filter = ['product']
def get_queryset(self, request):
return super().get_queryset(request).filter(product__user=request.user)
我希望特殊用户(供应商)在他们的 django 管理中看到他们自己的订单。每当我尝试过滤掉订购产品的用户时。这是代码。 我在我的产品模型中连接了供应商。我不知道如何将供应商与他们各自的订单联系起来。
class Order(models.Model):
STATUS = (
('New', 'New'),
('Accepted', 'Accepted'),
('Preparing', 'Preparing'),
('OnShipping', 'OnShipping'),
('Completed', 'Completed'),
('Canceled', 'Canceled'),
)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
code = models.CharField(max_length=5, editable=False)
first_name = models.CharField(max_length=10)
last_name = models.CharField(max_length=10)
phone = models.CharField(blank=True, max_length=20)
address = models.CharField(blank=True, max_length=150)
city = models.CharField(blank=True, max_length=20)
country = models.CharField(blank=True, max_length=20)
total = models.FloatField()
status=models.CharField(max_length=10,choices=STATUS,default='New')
ip = models.CharField(blank=True, max_length=20)
adminnote = models.CharField(blank=True, max_length=100)
create_at=models.DateTimeField(auto_now_add=True)
update_at=models.DateTimeField(auto_now=True)
class OrderProduct(models.Model):
STATUS = (
('New', 'New'),
('Accepted', 'Accepted'),
('Canceled', 'Canceled'),
)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
variant = models.ForeignKey(Variants, on_delete=models.SET_NULL,blank=True, null=True) # relation with variant
quantity = models.IntegerField()
price = models.FloatField()
amount = models.FloatField()
status = models.CharField(max_length=10, choices=STATUS, default='New')
create_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.product.title
class OrderAdmin(admin.ModelAdmin):
list_display = ['first_name', 'last_name','phone','city','total','status','create_at']
list_filter = ['status']
readonly_fields = ('user','address','city','country','phone','first_name','ip', 'last_name','phone','city','total')
can_delete = False
inlines = [OrderProductline]
class OrderProductAdmin(admin.ModelAdmin):
list_display = ['user', 'product','price','quantity','amount']
list_filter = ['product']
def get_queryset(self, request):
qs = super().get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(product=request.product)```
更改您的 ModelAdmin
子类:
class OrderProductAdmin(admin.ModelAdmin):
list_display = ['user', 'product','price','quantity','amount']
list_filter = ['product']
def get_queryset(self, request):
return super().get_queryset(request).filter(product__user=request.user)