在Django

时间:2018-08-15 19:17:42

标签: django django-rest-framework

假设我有三种型号:

class Product(models.Model):
    name = models.CharField(max_length=255)

class Plan(models.Model):
    products = models.ManyToManyField(
        Product, 
    )

class User(models.Model):
    plan = models.ForeignKey(Plan, on_delete=models.CASCADE)

我想查询一个用户列表,预取他们的计划,并用产品数量注释他们的计划。我正在尝试这样的事情:

plan_queryset = Plan.objects.all().annotate(num_product=Count('products'));

queryset = Plan.user_set \
   .prefetch_related(
       Prefetch('plan', 
          queryset=plan_queryset
       )
   )
serializer = UserSerializer(queryset, many=True)

但不幸的是,计划查询集未执行。将queryset传递给序列化程序时,出现错误,提示未定义“ num_product”。

在不对每个对象进行查询的情况下注释外键字段的最佳方法是什么? (n + 1个问题)。

更新:

示例序列化器:

class PlanSerializer(serializers.ModelSerializer):
    num_product=serializers.IntegerField()

    class Meta:
        model = Plan
        fields = ('num_product',)

class UserSerializer(serializers.ModelSerializer):
    plan = PlanSerializer(read_only=True)

    class Meta:
         model = User
         fields = ('plan',)

跟踪:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/rest_framework/fields.py", line 441, in get_attribute
    return get_attribute(instance, self.source_attrs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/fields.py", line 100, in get_attribute
    instance = getattr(instance, attr)
AttributeError: 'Plan' object has no attribute 'num_product'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/generics.py", line 201, in get
    return self.list(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/mixins.py", line 45, in list
    return self.get_paginated_response(serializer.data)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 738, in data
    ret = super(ListSerializer, self).data
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 656, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 656, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 500, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 487, in to_representation
    attribute = field.get_attribute(instance)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/fields.py", line 460, in get_attribute
    raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `num_product` on serializer `PlanSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Plan` instance.
Original exception text was: 'Plan' object has no attribute 'num_product'.

2 个答案:

答案 0 :(得分:0)

如果没有完整的堆栈跟踪等就很难说,但是,我认为问题出在这里:

class User(models.Model):
        plan = models.ForeignKey(Plan, on_delete=models.CASCADE)

您能帮我试试以下吗?

class User(models.Model):
        plan = models.ForeignKey(Plan, on_delete=models.CASCADE, related_name='user_plan')

然后使用以下内容:

queryset = User.objects.all() \
   .prefetch_related(
       Prefetch('user_plan__plan', 
          queryset=plan_queryset
       )
   )

答案 1 :(得分:0)

弄清楚了。该错误实际上在于查询的设置方式:

plan = Plan.objects.get(pk=plan_pk)

queryset = plan.user_set \

   .prefetch_related(
       Prefetch('plan', 
          queryset=plan_queryset
       )
   )

使用plan.user_set而不是直接在User对象管理器上查询,意味着Django使用的是来自plan.user_set的 cached Plan对象,而不是带有注释的预取查询。解决此问题的两种方法:

1)设置查询以使用用户对象管理器:

queryset = User.objects.filter(plan__pk=plan_pk) \
   .prefetch_related(
       Prefetch('plan', 
          queryset=plan_queryset
       )
   )

2)对原始的Plan.objects.get()调用进行注释,以使其具有必填字段。

长话短说:检查您的查询并确保未从先前的调用中缓存模型!