Python django 小问题提问,希望大佬解答下

小狼崽 · 2019年08月30日 · 926 次阅读

ProjectInfo:

class ProjectInfo(BaseTable):
    class Meta:
        verbose_name = '项目信息'
        db_table = 'ProjectInfo'

    project_name = models.CharField('项目名称', max_length=50, unique=True, null=False)
    responsible_name = models.CharField('负责人', max_length=20, null=False)
    test_user = models.CharField('测试人员', max_length=100, null=False)
    dev_user = models.CharField('开发人员', max_length=100, null=False)
    publish_app = models.CharField('发布应用', max_length=100, null=False)
    simple_desc = models.CharField('简要描述', max_length=100, null=True)
    other_desc = models.CharField('其他信息', max_length=100, null=True)
    objects = ProjectInfoManager()

ModuleInfo

class ModuleInfo(BaseTable):
    class Meta:
        verbose_name = '模块信息'
        db_table = 'ModuleInfo'

    module_name = models.CharField('模块名称', max_length=50, null=False)
    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.CASCADE)
    test_user = models.CharField('测试负责人', max_length=50, null=False)
    simple_desc = models.CharField('简要描述', max_length=100, null=True)
    other_desc = models.CharField('其他信息', max_length=100, null=True)
    objects = ModuleInfoManager()

ModuleInfoSerializer

class ModuleInfoSerializer(serializers.ModelSerializer):
    belong_project_id = serializers.IntegerField(source='ProjectInfo.id')

    class Meta:
        model = ModuleInfo
        fields = ('belong_project_id', 'test_user', 'simple_desc', 'other_desc', 'module_name')

    def create(self, validated_data):
        return ModuleInfo.objects.insert_module(**validated_data)

    def update(self, instance, validated_data):
        instance.module_name = validated_data.get('module_name', instance.module_name)
        instance.belong_project_id = validated_data.get('belong_project_id', instance.belong_project_id)
        instance.test_user = validated_data.get('test_user', instance.test_user)
        instance.simple_desc = validated_data.get('simple_desc', instance.simple_desc)
        instance.other_desc = validated_data.get('other_desc', instance.other_desc)
        instance.save()
        return instance

moduleQueryView

@pysnooper.snoop()
@api_view(['POST', 'GET'])
def moduleQueryView(request):
    if request.method == 'POST':
        module_name = request.data.get('module_name')
        if module_name is not None:
            query_Set = ModuleInfo.objects.filter(module_name__exact=module_name)
            print(query_Set)
            moduleQuerySerializer = ModuleInfoSerializer(query_Set, many=True)
            return Response({"code": "200", "msg": "请求成功", "error": "", "data": moduleQuerySerializer.data})
        query_Set = ModuleInfo.objects.all()
        moduleQuerySerializer = ModuleInfoSerializer(query_Set, many=True)
        return Response({"code": "200", "msg": "", "error": "", "data": moduleQuerySerializer.data})

一个查询接口,到时访问时,报错了,异常:

AttributeError: Got AttributeError when attempting to get a value for field `belong_project_id` on serializer `ModuleInfoSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `ModuleInfo` instance.
Original exception text was: 'ModuleInfo' object has no attribute 'ProjectInfo'.
[30/Aug/2019 12:22:42] "POST /api/module/query HTTP/1.1" 500 16990

只要去掉 ModuleInfoSerializer 中的 belong_project_id = serializers.IntegerField(source='ProjectInfo.id') 就可以访问成功,但是调用创建接口的时候,去掉刚才那个就会报错,不知道如何去修改代码,希望获得解答,该如何修改我的代码,网上搜索疑是我的数据源有问题

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册