简体   繁体   English

Django:通过相关的 OneToOne model 使用 through 参数序列化 ManyToMany 关系

[英]Django: Serialize a ManyToMany relationship with a through argument via a related OneToOne model

So the subject may or may not be contextually accurate but what I do know is it involves all of the above and I'm hitting a wall... I'm looking to get a Workspace model's owner URL directly on the Workspace something like:因此,该主题可能在上下文中准确,也可能不准确,但我所知道的是它涉及以上所有内容,我正在碰壁......我希望直接在Workspace上获得Workspace模型的所有者 URL,例如:

Desired Output所需 Output

{
    "url": "http://127.0.0.1:8000/api/workspaces/1/",
    "id": 1,
    "name": "Ws 1",
    "slug": "ws-1",
    "users": [
        "http://127.0.0.1:8000/api/users/2/",
        "http://127.0.0.1:8000/api/users/4/"
    ],
    "owner": "http://127.0.0.1:8000/api/users/2/"
}

Models.py模型.py

class Workspace(models.Model):
    name = models.CharField(max_length=80)
    users = models.ManyToManyField(UserModel, through="workspaces.WorkspaceUser", related_name="workspaces")

class WorkspaceUser(models.Mode):
    user = models.ForeignKey(User, related_name="workspace_users", on_delete=models.CASCADE)
    workspace = models.ForeignKey( Workspace, related_name="workspace_users",on_delete=models.CASCADE)

class WorkspaceOwner(BaseTimeStampedModel):
    workspace_user = models.OneToOneField(WorkspaceUser, on_delete=models.CASCADE)
    workspace = models.OneToOneField(Workspace, related_name="owner", on_delete=models.CASCADE)

Serializers.py序列化器.py

class WorkspaceSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Workspace
        fields = ["url", "id", "name", "slug", "users", "owner"]

class WorkspaceUserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = WorkspaceUser
        fields = ["url", "id", "user"]

class WorkspaceOwnerSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = WorkspaceOwner
        fields = ["url", "id", "user"]

How can I best serialize the workspace.owner.workspace_user.user url for the HyperlinkedModelSerializer ?我怎样才能最好地为HyperlinkedModelSerializer序列化workspace.owner.workspace_user.user url

I think you can use serializers.SerializerMethodField to get the url.我认为您可以使用serializers.SerializerMethodField来获取 url。

class WorkspaceSerializer(serializers.HyperlinkedModelSerializer):
    users = serializers.SerializerMethodField(read_only = True)
    owner = serializers.SerializerMethodField(read_only = True)

    class Meta:
        model = Workspace
        fields = ["url", "id", "name", "slug", "users", "owner"]

    def get_users(self, obj):
        cur_request = self.context['request']
        return [UserSerializer(x, context={'request': cur_request}).data['url'] for x in obj.users]

    def get_owner(self, obj):
        cur_request = self.context['request']
        return WorkspaceOwnerSerializer(obj.owner, context={'request': cur_request}).data['url']
   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django ManyToMany通过关系 - Django ManyToMany Through Relationship 在Django中获取与此模型相关的所有模型(通过ForeignKey,ManyToMany) - Get all models related to this model in django (via ForeignKey, ManyToMany) Django内联formset通过另一个模型过滤多种关系 - Django inline formset filters in manytomany relationship through another model 通过ManyToMany关系进行Django查询 - Django query with ManyToMany relationship through 在Django中访问与模型具有OneToOne关系的对象 - Accessing objects with OneToOne relationship to a model in Django 如何通过 Django 模型进行查询并在没有 ManyToMany 关系的情况下访问另一个 Django 模型? - How to query through a Django model and get access to another Django model without a ManyToMany relationship? Django ManyToMany通过模型实现 - Django ManyToMany with through model implementation Django通过“通过”关系表从模型中获取所有相关对象 - Django get all related objects from model with 'through' relationship tables 具有 OneToOne 关系和相关字段的 django factory boy factory - django factory boy factory with OneToOne relationship and related field 序列化通过另一个模型关联的模型字段 - Serialize model fields related through another model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM