DRF序列化程序 - 通过字段的多对多,对象没有属性

时间:2017-09-07 19:27:55

标签: python django serialization many-to-many django-rest-framework

我的模型与直通模型有多对多的关系。我正在尝试设置DRF序列化程序来显示此数据,但每当我尝试呈现API时,我都会收到错误消息。

# models.py - simplified

class Person(models.Model):
    first_name = models.CharField(max_length=250)
    last_name = models.CharField(max_length=250)
    status = models.IntegerField(choices=STATUS_CHOICES)
    village = models.ForeignKey(Village)
    gender = models.IntegerField(choices=GENDER_CHOICES)


class Case(models.Model):
    summary = models.TextField()
    session = models.ForeignKey(Session, on_delete=models.CASCADE)
    case_type = models.ForeignKey(CaseType)
    court_type = models.IntegerField(choices=COURT_TYPES)
    verdict = models.ForeignKey(Verdict)
    litigants = models.ManyToManyField(Person, through='Litigant', related_name='litigants')


class Litigant(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    case = models.ForeignKey(Case, on_delete=models.CASCADE)
    role = models.ForeignKey(Role)
    fine = models.ForeignKey(Money, null=True, related_name='fine')

我的serializers.py看起来像这样:

# serializers.py - simplified

class LitigantSerializer(FlexFieldsModelSerializer):

    class Meta:
        model = Litigant
        fields = ('id', 'person', 'case', 'role', 'fine')


class CaseSerializer(FlexFieldsModelSerializer):

    litigants = LitigantSerializer(many=True, read_only=True)

    class Meta:
        model = Case
        fields = ('id', 'summary', 'session', 'case_type', 'court_type', 'verdict', 'litigants')


class PersonSerializer(FlexFieldsModelSerializer):

    class Meta:
        model = Person
        fields = ('id','first_name', 'last_name', 'village', 'status', 'gender')

我的views.py是:

# views.py - simplified.

class PersonViewSet(FlexFieldsModelViewSet):
    queryset = Person.objects.all().order_by('village__name', 'last_name', 'first_name')
    serializer_class = PersonSerializer


class CaseViewSet(FlexFieldsModelViewSet):
    queryset = Case.objects.all().order_by('session__village__name', 'session__date', 'court_type')
    serializer_class = CaseSerializer


class LitigantViewSet(FlexFieldsModelViewSet):
    queryset = Litigant.objects.all().order_by('case__session__village__name', 'case__session__date', 'person__last_name',
                                          'person__first_name')
    serializer_class = LitigantSerializer

但是,当我导航到api/cases/时,我收到以下错误:

'Person' object has no attribute 'person'

我不确定为什么要使用' person'作为Person对象的标识符,而不是其PK。我在这里缺少什么吗?

这是请求的堆栈跟踪。

Traceback:

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/viewsets.py" in view
  90.             return self.dispatch(request, *args, **kwargs)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
  489.             response = self.handle_exception(exc)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/views.py" in handle_exception
  449.             self.raise_uncaught_exception(exc)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
  486.             response = handler(request, *args, **kwargs)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_flex_fields/views.py" in list
  28.       return super(FlexFieldsMixin, self).list(request, *args, **kwargs)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/mixins.py" in list
  45.             return self.get_paginated_response(serializer.data)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/serializers.py" in data
  739.         ret = super(ListSerializer, self).data

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/serializers.py" in data
  263.                 self._data = self.to_representation(self.instance)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/serializers.py" in to_representation
  657.             self.child.to_representation(item) for item in iterable

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/serializers.py" in <listcomp>
  657.             self.child.to_representation(item) for item in iterable

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/serializers.py" in to_representation
  501.                 ret[field.field_name] = field.to_representation(attribute)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/serializers.py" in to_representation
  657.             self.child.to_representation(item) for item in iterable

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/serializers.py" in <listcomp>
  657.             self.child.to_representation(item) for item in iterable

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/serializers.py" in to_representation
  488.                 attribute = field.get_attribute(instance)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/relations.py" in get_attribute
  179.         return get_attribute(instance, self.source_attrs)

File "/home/vagrant/.virtualenvs/medievaldb/lib/python3.4/site-packages/rest_framework/fields.py" in get_attribute
  103.                 instance = getattr(instance, attr)

Exception Type: AttributeError at /api/cases/
Exception Value: 'Person' object has no attribute 'person'

0 个答案:

没有答案