DRF嵌套序列化程序-对象没有属性

时间:2020-09-01 12:01:20

标签: django-rest-framework nested serialization

我是django rest框架和python的新手。这是我第一次在这里问一个问题。感谢您的帮助。

--------------我的模特

[ERROR] Failed to execute goal org.apache.hadoop:hadoop-maven-plugins:3.2.0:cmake-compile (cmake-compile) on project hadoop-hdfs-native-client: CMake failed with error code 1 -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.hadoop:hadoop-maven-plugins:3.2.0:cmake-compile (cmake-compile) on project hadoop-hdfs-native-client: CMake failed with error code 1
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: CMake failed with error code 1
    at org.apache.hadoop.maven.plugin.cmakebuilder.CompileMojo.runCMake (CompileMojo.java:172)
    at org.apache.hadoop.maven.plugin.cmakebuilder.CompileMojo.execute (CompileMojo.java:97)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)

-------------------序列化器


class defined_views(models.Model):
    name = models.CharField(max_length=150)

class defined_permissions(models.Model):
    name = models.CharField(max_length=150)

class menuler(models.Model):
    adi= models.CharField(max_length=150, null=True)
    sira= models.PositiveIntegerField(null=True, blank=True)
    parentId= models.ForeignKey('self', null=True, on_delete=models.CASCADE)
    viewId= models.ForeignKey(defined_views, null=True, on_delete=models.CASCADE)
    menu_type= models.CharField(max_length=150, null=True)

class user_menu_permission(models.Model):
    userId = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
    menuID = models.ForeignKey(menuler, null=False, on_delete=models.CASCADE)
    permissionID = models.ForeignKey(defined_permissions, null=True, on_delete=models.CASCADE)

我得到这个错误

”尝试在序列化程序class menulerSerializer(serializers.ModelSerializer): class Meta: model = menuler fields = ['id', 'adi', 'sira', 'parentId', 'viewId', 'menu_type'] class user_menu_permissionSerializer(serializers.ModelSerializer): menu = menulerSerializer() class Meta: model = user_menu_permission fields = ['menuID', 'permissionID', 'menu'] class UserSerializer(serializers.ModelSerializer): menuler = user_menu_permissionSerializer( source='user_menu_permission_set', many=True) class Meta: model = User fields = ['id', 'username', 'menuler'] ------------------and view class userView(APIView): def get(self, request): queryset = User.objects.all() serializer = UserSerializer(queryset, many=True) return Response(serializer.data) 上获取字段menu的值时发生AttributeError。 序列化程序字段的名称可能不正确,并且与user_menu_permissionSerializer实例上的任何属性或键都不匹配。 原始异常文本为:'user_menu_permission'对象没有属性'menu'。”


如果我这样更改user_menu_permissionSerializer

user_menu_permission

我得到了这样的结果,但我也希望菜单像树一样..但是很幸运...

class user_menu_permissionSerializer(serializers.ModelSerializer):
    class Meta:
        model = user_menu_permission
        fields = ['menuID', 'permissionID'] 

2 个答案:

答案 0 :(得分:0)

尝试以下类似方法:

serializer.py:

class user_menu_permissionSerializer(serializers.ModelSerializer):
    menu = menulerSerializer(many=True)
    class Meta:
        model = user_menu_permission
        fields = ['menuID', 'permissionID', 'menu'] 

views.py:

class userView(APIView):

    def get(self, request):
        data = User.objects.all()
        serializer = UserSerializer(data, many=True)
        return Response(serializer.data)

答案 1 :(得分:0)

我已经解决了以下问题

模型

class defined_views(models.Model):
    name = models.CharField(max_length=150)

class defined_permissions(models.Model):
    name = models.CharField(max_length=150)

class menuler(models.Model):
    adi= models.CharField(max_length=150, null=True)
    sira= models.PositiveIntegerField(null=True, blank=True)
    parentId= models.ForeignKey('self', null=True, on_delete=models.CASCADE)
    viewId= models.ForeignKey(defined_views, null=True, on_delete=models.CASCADE)
    menu_type= models.CharField(max_length=150, null=True)
    permissionID = models.ManyToManyField(defined_permissions, through='user_menu_permission')
    userId = models.ManyToManyField(User, through='user_menu_permission')

class user_menu_permission(models.Model):
    # userId = models.ForeignKey(User, related_name='kullanici_menuler', null=False, on_delete=models.CASCADE)
    userId = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
    menuID = models.ForeignKey(menuler,null=False, on_delete=models.CASCADE)
    permissionID = models.ForeignKey(defined_permissions, null=True, on_delete=models.CASCADE)

序列化器

class defined_permissionsSerializer(serializers.ModelSerializer):
    class Meta:
        model = defined_permissions
        fields = ['id', 'name'] 

class defined_viewsSerializer(serializers.ModelSerializer):
    class Meta:
        model = defined_views
        fields = ['id', 'name'] 

class menulerSerializer(serializers.ModelSerializer):
    class Meta:
        model = menuler
        fields = ['id',  'adi', 'sira', 'parentId', 'viewId', 'menu_type'] 
        # fields = ['adi'] 

class user_menu_permissionSerializer(serializers.ModelSerializer):
    # menu = menulerSerializer(many=True)

    class Meta:
        model = user_menu_permission
        fields = ['menuID', 'permissionID'] 


class UserSerializer(serializers.ModelSerializer):
    # menuler = user_menu_permissionSerializer(
    #     source='user_menu_permission_set', many=True)
    # kullanici_menuler = user_menu_permissionSerializer(many=True)
    kullanici_menuler= menulerSerializer(source='menuler_set', many=True)
    class Meta:
        model = User
        fields = ['id', 'username', 'kullanici_menuler']

和结果

[
    {
        "id": 1,
        "username": "admin",
        "kullanici_menuler": []
    },
    {
        "id": 2,
        "username": "tayfun.uzun",
        "kullanici_menuler": [
            {
                "id": 2,
                "adi": "Yapılacaklar Listesi",
                "sira": 1,
                "parentId": 1,
                "viewId": 1,
                "menu_type": "view"
            },
            {
                "id": 3,
                "adi": "Raporlamalar",
                "sira": 2,
                "parentId": 1,
                "viewId": 2,
                "menu_type": "view"
            },
            {
                "id": 5,
                "adi": "Süreç İşleyişleri",
                "sira": 1,
                "parentId": 4,
                "viewId": 3,
                "menu_type": "view"
            }
        ]
    }
]