【问题标题】:How can I join two serilaizers in django rest framework that have a similar field value如何在 django rest 框架中加入两个具有相似字段值的 serilaizer
【发布时间】:2022-01-29 00:37:11
【问题描述】:

我的 Django Restframework 中有两个模型。在我看来,我想获取所有属性,并且对于每个属性,我都会获取创建它的用户的个人资料数据。

我怎样才能做到这一点?

示例:

#models.py
from django.contrib.auth.models import User

class Profile(models.Model):
     user = models.ForeignKey(User, on_delete=models.CASCADE)
     mobile = models.CharField(max_length=200)
     location = models.CharField(max_length=200)


class Properties(models.Model):
    title = models.CharField(max_length=200)
    price = models.CharField(max_length=200)
    category = models.CharField(max_length=200)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)


#serializers.py
class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = '__all__'


class PropertySerializer(serializers.ModelSerializer):
    class Meta:
        model = Property
        fields = [
         'title','price','category','created_by'   
        ]


#views.py
class PropertiesView(generics.ListAPIView):
    serializer = PropertySerializer
    queryset = Property.objects.all()

【问题讨论】:

    标签: django django-models django-rest-framework django-views


    【解决方案1】:

    如果您想在您的属性中获取个人资料数据,请查看下方 -

    class PropertySerializer(serializers.ModelSerializer):
        mobile = serializers.SerializerMethodField()
        location = serializers.SerializerMethodField()
    
        class Meta:
            model = Property
            fields = [
             'title','price','category','created_by', "mobile", "location"
            ]
    
        def get_mobile(self, instance):
            return Profile.objects.get(user = instance.created_by).mobile
    
        def get_location(self, instance):
            return Profile.objects.get(user = instance.created_by).location
    

    【讨论】:

      【解决方案2】:

      尝试:

      如果您想要完整的个人资料数据,那么 -

      首先在properties model(createby field)中定义相关的名字,比如-

      class Profile(models.Model):
          ---
          ---
      
      class Properties(models.Model):
          ---
        created_by = 
            models.ForeignKey(User, on_delete=models.CASCADE, related_name = "abc")
      

      然后在序列化程序中使用相关名称,并将其包含在 - 等字段中

      class PropertySerializer(serializers.ModelSerializer):
          abc = ProfileSerializer()
          class Meta:
              model = Property
              fields = [
               'title','price','category','created_by', 'abc'
              ]
      

      如果您想要特定的个人资料数据,那么 -

      class ProfileSerializer(serializers.ModelSerializer):
          class Meta:
              model = Profile
              fields = '__all__'
      
      
      class PropertySerializer(serializers.ModelSerializer):
          mobile = serializers.SerializerMethodField()
      
          class Meta:
              model = Property
              fields = [
               'title','price','category','created_by', "mobile",
              ]
      
          def get_mobile(self, instance):
              return instance.created_by.mobile if instance.created_by else ''
      

      【讨论】:

      • 以上都不适合我。案例 1 错误:字段名称 abc 对模型 Property 无效案例 2 错误:“用户”对象没有属性“移动”
      • 好的,我对用户和配置文件模型类感到困惑。等一下,我会更新我的答案
      • 检查我的更新答案以获取属性中配置文件数据的值。
      • 将属性序列化器中的 abc = ProfileSerializer() 替换为 abc = UserSerializer() 以从用户模型中获取所有数据。但是,这不会从配置文件中获取数据。
      • 您应该扩展用户类以创建额外的字段,目前您正在配置文件模型中创建这些字段。拥有一个表总是比为相同的数据维护两个表有用。 Exting 用户类将有一个单一的信息源。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 2016-08-06
      • 2013-03-06
      • 2018-07-21
      • 2016-11-09
      相关资源
      最近更新 更多