I want special users (vendors) to see their own orders in their django admin. When ever I tried that it filters out the user that ordered the products. Here are the codes. I connected the vendors in my product model. I can't figure out how to connect vendors with their respective orders.
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)```
CodePudding user response:
Change your ModelAdmin
subclass:
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)