嵌套序列化程序Django Rest Fram

2024-06-17 07:55:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个模型阶段和应用。我遇到了这样一个错误:

Exception Type: ImportError

Exception Value: cannot import name ApplicationSerializer

Exception Location: /Users/jojjen/workspace/indusaction/vms/stages/serializers.py in , line 7

我猜这个错误是由于序列化程序中的循环导入造成的。但我不知道怎么解决这个问题。在

如果我删除ApplicationSerializerStageSerializer导入,它就可以工作了。但是我需要访问我正在构建的模板中stage应用程序的某些属性。在

代码如下:

阶段/模型.py以下内容:

from django.db import models

from core.models import TimeStampedModel

from applications.models import Application

from authentication.models import Account


class Stage(TimeStampedModel):
    name = models.CharField(max_length=20)

    application = models.ForeignKey(Application, null=True, related_name='stages')

    assignee = models.ForeignKey(Account, null=True)

    def __unicode__(self):
        return self.name

应用/模型.py以下内容:

^{pr2}$

阶段/序列化程序.py在

from rest_framework import serializers

from stages.models import Stage

from authentication.serializers import AccountSerializer

from applications.serializers import ApplicationSerializer


class StageSerializer(serializers.ModelSerializer):
    assignee = AccountSerializer(read_only=True, required=False)
    application = ApplicationSerializer(read_only=True, required=False)

    class Meta:
        model = Stage
        fields = ('id', 'name', 'assignee', 'created_at', 'updated_at',
                  'application',)
        read_only_fields = ('created_at', 'updated_at', 'assignee',
                            'application',)

        def get_validation_exclusions(self, *args, **kwargs):
            exclusions = super(StageSerializer,
                               self).get_validation_exclusions()

            return exclusions + ['assignee', 'application']

应用/序列化程序.py以下内容:

from rest_framework import serializers

from applications.models import Application

from organizations.serializers import OrganizationSerializer
from authentication.serializers import AccountSerializer
from stages.serializers import StageSerializer
from applicants.serializers import ApplicantSerializer


class ApplicationSerializer(serializers.ModelSerializer):
    organization = OrganizationSerializer(read_only=True, required=False)
    creator = AccountSerializer(read_only=True, required=False)
    users = AccountSerializer(read_only=True, required=False, many=True)
    stages = StageSerializer(read_only=True, required=False, many=True)
    applicant_set = ApplicantSerializer(read_only=True, required=False, many=True)

    class Meta:
        model = Application

        fields = ('id', 'title', 'details', 'organization', 'stages',
                  'creator', 'archived', 'users', 'applicant_set')
        read_only_fields = ('id', 'organization', 'users', 'applicant_set',
                            'created_at', 'updated_at', 'stages')

    def get_validation_exclusions(self, *args, **kwargs):
        exclusions = super(ApplicatiionSerializer,
                           self).get_validation_exclusions()

        return exclusions + ['organization', 'users', 'creator', 'stage_set', 'applicant_set']

Tags: namefromimportselffalsetrueonlyread
1条回答
网友
1楼 · 发布于 2024-06-17 07:55:20

此代码无效,不能有循环导入和依赖项。对于Python开发的Django-Rest框架来说,这甚至不是真的。你必须改变你的设计来移除这个循环依赖。在

任何语言中的循环依赖都是无限递归。在

看看这个库-https://github.com/heywbj/django-rest-framework-recursive/tree/master/rest_framework_recursive。这可以用来模拟循环依赖关系,就像在树或链接列表中一样。在

但我还是建议你重新考虑一下你的设计。Read why circular dependency is a sign of poor design. 如果不可能,请将相互依赖的组件放入同一个模块中。在

相关问题 更多 >