Django REST嵌套序列化器

huangapple go评论55阅读模式
英文:

Django REST nested serializers

问题

I'm new to Django and Django Rest here.

我是Django和Django Rest的新手。

I created 3 models (see below).

我创建了3个模型(如下所示)。

I'm able to create some easy and basic serializers for each of those models, but I would like to create a more complex serializer for the CustomUser models than returns for each user it's roles.

我能够为这些模型中的每一个创建一些简单和基本的序列化程序,但我想为CustomUser模型创建一个更复杂的序列化程序,以返回每个用户的角色。

I've read the "Serializer relations" section in the doc. It's still not very clear how to achieve it.

我已经阅读了文档中的“序列化器关系”部分。如何实现还不是很清楚。

In the examples they use 2 models relations, but if I'm not wrong, I have 3, making things more complicated for me to understand.

在示例中,他们使用了2个模型关系,但如果我没有理解错的话,我有3个模型,这让我更难理解。

Could someone give me some tips to do it?

有人能给我一些提示吗?

class CustomUser(AbstractUser):

class CustomUser(AbstractUser):

email = models.EmailField(unique=True)

email = models.EmailField(unique=True)

username = models.CharField(max_length=100)

username = models.CharField(max_length=100)

USERNAME_FIELD = 'email'

USERNAME_FIELD = 'email'

class Meta:

class Meta:

db_table = "MyUsers"

db_table = "MyUsers"

def str(self):

def str(self):

return self.email

return self.email

class Roles(models.Model):

class Roles(models.Model):

id = models.AutoField(primary_key=True)

id = models.AutoField(primary_key=True)

label = models.CharField(max_length=100, related_name="roles_label")

label = models.CharField(max_length=100, related_name="roles_label")

class Meta:

class Meta:

db_table = "roles"

db_table = "roles"

def str(self):

def str(self):

return self.label

return self.label

class RolesUsers(models.Model):

class RolesUsers(models.Model):

id_user = models.ForeignKey(CustomUser, related_name="id_user", on_delete=models.CASCADE)

id_user = models.ForeignKey(CustomUser, related_name="id_user", on_delete=models.CASCADE)

id_role = models.ForeignKey(Roles, related_name="id_role", on_delete=models.CASCADE)

id_role = models.ForeignKey(Roles, related_name="id_role", on_delete=models.CASCADE)

class Meta:

class Meta:

db_table = "roles_users"

db_table = "roles_users"

unique_together = (("id_user", "id_role"),)

unique_together = (("id_user", "id_role"),)

def str(self):

def str(self):

return '{}: {}'.format(self.id_role, self.id_user)

英文:

I'm new to Django and Django Rest here.

I created 3 models (see below).

I'm able to create some easy and basic serializers for each of those models, but I would like to create a more complex serializer for the CustomUser models than returns for each user it's roles.

I've read the "Serializer relations" section in the doc. It's still not very clear how to achieve it.

In the examples they uses 2 models relations, but if I'm not wrong, I have 3, making things more complicated for me to understand.

Could someone give me some tips to do it?

class CustomUser(AbstractUser):

    email = models.EmailField(unique=True)
    username = models.CharField(max_length=100)
    USERNAME_FIELD = 'email'

    class Meta:
        db_table = "MyUsers"
    def __str__(self):
        return self.email

class Roles(models.Model):
    id = models.AutoField(primary_key=True)
    label = models.CharField(max_length=100,related_name="roles_label")
    class Meta:
        db_table = "roles"
    def __str__(self):
        return self.label

class RolesUsers(models.Model):
    id_user = models.ForeignKey(CustomUser,related_name="id_user", on_delete=models.CASCADE)
    id_role = models.ForeignKey(Roles, related_name="id_role", on_delete=models.CASCADE)
    class Meta:
        db_table = "roles_users"
        unique_together = (("id_user", "id_role"),)
    def __str__(self):
        return '{}: {}'.format(self.id_role, self.id_user)

答案1

得分: 0

相关名称应具有有意义的名称

class RolesUsers(models.Model):
    user = models.ForeignKey(CustomUser, related_name="user_role", on_delete=models.CASCADE)
    role = models.ForeignKey(Roles, related_name="user_role", on_delete=models.CASCADE)
    class Meta:
        db_table = "roles_users"
        unique_together = (("user", "role"),)
    def __str__(self):
        return '{}: {}'.format(self.role, self.user)

你可以创建用户序列化器

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomUser
        fields = ('id', 'user_role', 'email', 'username')
英文:

related name should be meaning full name

class RolesUsers(models.Model):
    user = models.ForeignKey(CustomUser,related_name="user_role", on_delete=models.CASCADE)
    role = models.ForeignKey(Roles, related_name="user_role", on_delete=models.CASCADE)
    class Meta:
        db_table = "roles_users"
        unique_together = (("id_user", "id_role"),)
    def __str__(self):
        return '{}: {}'.format(self.id_role, self.id_user)

you can create user serializer


class UserSerializer(serializer.ModelSerializers):
   class Meta:
         model = CustomUser
         fields = ('id', 'user_role', 'email', 'username')

huangapple
  • 本文由 发表于 2020年1月6日 17:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定