【发布时间】:2013-11-22 10:10:56
【问题描述】:
我正在使用 Django,我想在其中使用过滤器
我的产品和公司型号是
class Product(models.Model):
name = models.CharField(max_length=200)
companyId = models.ForeignKey(Comapany)
class Company(models.Model):
domain = models.CharField(max_length=200)
我想根据当前用户的 companyId 检索产品。所以我已经像这样实现了我的观点..
class ListProducts(APIView):
authentication_classes = (authentication.TokenAuthentication,)
permission_classes = (permissions.IsAdminUser,)
def get(self, request):
if request.user.is_authenticated():
userCompanyId = request.user.get_profile().companyId
products = Product.objects.filter(companyId__id__exact = userCompanyId)
serializer = ProductSerializer(products)
return Response(serializer.data)
我的产品数据
{
"_id": ObjectId("5284ceaae9cfff79368e1f29"),
"companyId": "528458c4bbe7823947b6d2a3",
"name": "Apple Juice"
}
{
"_id": ObjectId("5267bb4ebbe78220588b4567"),
"companyId": "52674f02bbe782d5528b4567",
"name": "Small Soft & Moist Cranberry"
}
我的公司资料
{
"_id": ObjectId("528458c4bbe7823947b6d2a3"),
"domain": "Manufacturing"
}
{
"_id": ObjectId("52674f02bbe782d5528b4567"),
"domain": "Manufacturing"
}
我的输出为[]
问题是,在研究 django doc 中的过滤器时,我无法获得它。所以请帮助我..
【问题讨论】:
-
粘贴您的用户配置文件模型定义?
-
类 UserProfile(models.Model): user = models.ForeignKey(User,unique=True) companyId = models.IntegerField()
-
我已经用公司模型和新视图更新了我的问题。我没有收到上一个错误,而是将 [] 作为输出
标签: python django mongodb django-rest-framework