I am working on a Web App similar to a Web E-commerce site where various persons from different companies can enter their products in the admin page of my web app. I am trying to get each user to view in the Admin page, only the products of the company he belongs, in more detail the products that belong to a company with VAT the same as the user's company vat.
我正在开发类似于Web电子商务网站的Web应用程序,来自不同公司的不同人员可以在我的Web应用程序的管理页面中输入他们的产品。我试图让每个用户在管理页面中查看,只查看他所属公司的产品,更详细地说,属于公司的产品,其增值税与用户的公司增值税相同。
My models schema is sth like. Product with a Foreign Key to Line of Products, which has a Foreign Key to Catalogue, which finally has a Foreign Key to Company. So in order to achieve the above I came up with sth like the code below:
我的模型模式就像。带产品系列外键的产品,具有目录外键,最终具有公司外键。所以为了实现上述目的,我想出了类似下面的代码:
**admin.py**
class ProductAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(ProductAdmin, self).queryset(request)
if request.user.is_superuser:
return qs
else:
try:
comp = Company.objects.get(vat_number = request.user.get_profile().organization_vat)
comp_catalogs = Catalog.objects.filter(company_id = comp.id)
print '3. ', comp_catalogs
lista = []
for cat in comp_catalogs:
lista.append(cat.id)
print '4. ', lista
lines = Line.objects.filter(catalog_id__in=lista)
lista = []
for line in lines:
lista.append(line.id)
return qs.filter(line_id__in=lista)
except:
return qs.none()
Is there a way to make the above series of queries in a nicer way (less code)?
有没有办法以更好的方式(更少的代码)进行上述一系列查询?
1 个解决方案
#1
1
This should do it:
这应该这样做:
def queryset(self, request):
qs = super(ProductAdmin, self).queryset(request)
if request.user.is_superuser:
return qs
else:
return qs.filter(line__catalog__company__vat_number=request.user.get_profile().organization_vat)
#1
1
This should do it:
这应该这样做:
def queryset(self, request):
qs = super(ProductAdmin, self).queryset(request)
if request.user.is_superuser:
return qs
else:
return qs.filter(line__catalog__company__vat_number=request.user.get_profile().organization_vat)