如何通过 Django rest 使用 related_name 获取序列化程序中的特定字段
how to get specific field in serialiser with related_name by Django rest
我有一个名为 ContactPhone 的模型,在模型 link 中使用字段 lead_id 和 ralated_name phones
领导模型
class ContactPhone(models.Model):
phone_number = PhoneNumberField(blank=True, default='')
phone_type = models.CharField(
max_length=100,
blank=True,
choices=ContactPhoneInterface.phone_number_types,
default=PhoneNumbers.mobile.name
)
contact_id = models.ForeignKey(
Contact,
on_delete=models.CASCADE,
null=True,
related_name='phones_c'
)
lead_id = models.ForeignKey(
Lead,
on_delete=models.CASCADE,
null=True,
related_name='phones'
)
通过这个 serilizer class 我得到了 ContactPhone 的 id table,但我需要在 ContactPhone 中得到 phone_number 字段。
class LeadListSerializer(serializers.ModelSerializer):
class Meta:
model = Lead
fields = (
'id',
'full_name',
'responsible_id',
'phones',
'emails',
)
尝试了不同的方法来解决这个问题,但没有用。在截图中可以看到此时的响应,ContactPhone
的id
如果您想 return 相关模型中的特定字段列表,您可以使用 serializers.SlugRelatedField
class LeadListSerializer(serializers.ModelSerializer):
phones = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field='phone_number'
)
class Meta:
model = Lead
fields = (
'id',
'full_name',
'responsible_id',
'phones',
'emails',
)
编辑:ContactPhone.phone_number 字段不可序列化
一个选项是将 ContactPhone 模型的 __str__
方法设置为 return phone 号码并使用 StringRelatedField
class ContactPhone(models.Model):
...
def __str__(self):
return str(self.phone_number)
class LeadListSerializer(serializers.ModelSerializer):
phones = serializers.StringRelatedField(
many=True,
read_only=True,
)
class Meta:
model = Lead
fields = (
'id',
'full_name',
'responsible_id',
'phones',
'emails',
)
或者将 return 的 phone_number 字符串添加到 ContactPhone 模型并在 SlugRelatedField 中使用它是潜在的选择
class ContactPhone(models.Model):
...
@property
def phone_number_str(self):
return str(self.phone_number)
class LeadListSerializer(serializers.ModelSerializer):
phones = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field='phone_number_str'
)
class Meta:
model = Lead
fields = (
'id',
'full_name',
'responsible_id',
'phones',
'emails',
)
我有一个名为 ContactPhone 的模型,在模型 link 中使用字段 lead_id 和 ralated_name phones
领导模型class ContactPhone(models.Model):
phone_number = PhoneNumberField(blank=True, default='')
phone_type = models.CharField(
max_length=100,
blank=True,
choices=ContactPhoneInterface.phone_number_types,
default=PhoneNumbers.mobile.name
)
contact_id = models.ForeignKey(
Contact,
on_delete=models.CASCADE,
null=True,
related_name='phones_c'
)
lead_id = models.ForeignKey(
Lead,
on_delete=models.CASCADE,
null=True,
related_name='phones'
)
通过这个 serilizer class 我得到了 ContactPhone 的 id table,但我需要在 ContactPhone 中得到 phone_number 字段。
class LeadListSerializer(serializers.ModelSerializer):
class Meta:
model = Lead
fields = (
'id',
'full_name',
'responsible_id',
'phones',
'emails',
)
尝试了不同的方法来解决这个问题,但没有用。在截图中可以看到此时的响应,ContactPhone
的id如果您想 return 相关模型中的特定字段列表,您可以使用 serializers.SlugRelatedField
class LeadListSerializer(serializers.ModelSerializer):
phones = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field='phone_number'
)
class Meta:
model = Lead
fields = (
'id',
'full_name',
'responsible_id',
'phones',
'emails',
)
编辑:ContactPhone.phone_number 字段不可序列化
一个选项是将 ContactPhone 模型的 __str__
方法设置为 return phone 号码并使用 StringRelatedField
class ContactPhone(models.Model):
...
def __str__(self):
return str(self.phone_number)
class LeadListSerializer(serializers.ModelSerializer):
phones = serializers.StringRelatedField(
many=True,
read_only=True,
)
class Meta:
model = Lead
fields = (
'id',
'full_name',
'responsible_id',
'phones',
'emails',
)
或者将 return 的 phone_number 字符串添加到 ContactPhone 模型并在 SlugRelatedField 中使用它是潜在的选择
class ContactPhone(models.Model):
...
@property
def phone_number_str(self):
return str(self.phone_number)
class LeadListSerializer(serializers.ModelSerializer):
phones = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field='phone_number_str'
)
class Meta:
model = Lead
fields = (
'id',
'full_name',
'responsible_id',
'phones',
'emails',
)