如何为具有外键的模型创建序列化程序来访问外键表数据?

Ho to create serializer for model having foreign keys to access that foreign keys tables data also?

我想使用序列化程序以更有效的方式重写以下 API。 在下面的API中,usergroup是外键。

我想 return groupuser 的所有匹配数据使用 Serializer。

如何针对这种情况设计序列化程序。

@api_view(['GET'])
def get_post(request, group_id, post_type, post_limit):
    if request.method == 'GET':
        print('group id = ')
        print(group_id)
        # data = GroupPostsModel.objects.filter(
        #     group_id=group_id) & GroupPostsModel.objects.filter(post_type=post_type).order_by('-time_stamp')

        try:
            data = GroupPostsModel.objects.filter(
                group_id=group_id, post_type=post_type).order_by('-time_stamp')[post_limit:post_limit+3]
            # data = data[0:3] model.objects.filter(book = 'bible')
            post_arr_obj = []
            for post in data:
                comments_count = GroupPostCommentsModel.objects.filter(
                post_id=post.id).count()
                print('post id = '+str(post.id))
                post_dict = {
                    'post_id': str(post.id),
                    'post_text': str(post.post_text),
                    'post_type': str(post.post_type),
                    'image': str(post.image),
                    'document': str(post.document),
                    'likes': str(post.likes),
                    'group_id': str(post.group.id),
                    'user_id': str(post.user.id),
                    'time_stamp': str(post.time_stamp),
                    'profile_pic': str(post.user.profile_pic),
                    'first_name': str(post.user.first_name),
                    'last_name': str(post.user.last_name),
                    'gender': str(post.user.gender),
                    'comments_count': str(comments_count)
                }
                post_arr_obj.append(post_dict)
        except Exception as e:
            print('get_post exception : '+str(e))

        resp = {
            'resp': post_arr_obj
        }
        # group_post_serializer = GroupPostsSerializer(data, many=True)
        return Response(resp)
    else:
        return Response({'msg': 'Only GET request allowed..!'})

关系

One user can join/create many groups

One group can have mane posts

One user can post many posts

用户模型

class UserModel(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    gender = models.CharField(max_length=6)
    college_name = models.CharField(max_length=50)
    user_id = models.CharField(max_length=30,unique = True)
    user_password = models.CharField(max_length=20)
    profile_pic = models.ImageField(blank=True, null=True, upload_to='profile_pic/')

    class Meta:
        db_table = 'user'

组模型

class GroupsModel(models.Model):
    group_name = models.CharField(max_length=20, unique=True)
    group_description = models.CharField(max_length=50)
    group_joining_link = models.CharField(max_length=50, default='', unique=True)
    user_id = models.ManyToManyField(UserModel, through='UserModelGroupsModel', related_name='u_g')

    class Meta:
        db_table = 'groups'

GroupPostsModel

class GroupPostsModel(models.Model):
    post_text = models.CharField(max_length=1000)
    post_type = models.CharField(max_length=20)
    image = models.ImageField(blank=True, null=True, upload_to='post_images/')
    document = models.FileField(blank=True,null=True, upload_to='post_documents/')
    likes = models.IntegerField(default=0)
    time_stamp = models.DateTimeField(auto_now=True)
    group = models.ForeignKey(GroupsModel, on_delete=models.CASCADE)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE)

    def delete(self):
       if self.image:
          if os.path.isfile(self.image.path):
             os.remove(self.image.path)

       if self.document:
          if os.path.isfile(self.document.path):
             os.remove(self.document.path)
       
       super().delete()


    class Meta:
        db_table = 'group_posts'

您可以使用 Django Rest Framework ModelSerializer 来执行此操作。使用模型 GroupPostsModel

创建序列化程序
class GroupPostsSerializer(serializers.ModelSerializer):

    id = IntegerField()
    post_text = CharField()
    ...
    group_id = IntegerField(source="group.id")
    ...

    class Meta:
        model = GroupPostsModel
        fields = [
            'id'
            'post_text'
            ...
        ]


对于 comments_count,您可以使用 SerializerMethodField() 定义序列化程序将用于该字段的方法。

使用您的帖子初始化序列化程序

serializer = GroupPostsSerializer(data, many=True)

然后访问数据

resp = {
            'resp': serializer.data
        }