gpt4 book ai didi

python - 如何从序列化器中的另一个模型访问外键字段

转载 作者:太空宇宙 更新时间:2023-11-03 20:10:00 26 4
gpt4 key购买 nike

我正在尝试从序列化器中的另一个模型访问某些字段,但无论如何都无法使其工作。以下是我的做法。这是我的模型:

class Category(models.Model):
name = models.CharField(max_length=256)

class Meta:
ordering = ('name',)

def __str__(self):
return self.name


class Contact(models.Model):
first_name = models.CharField(max_length=256)
last_name = models.CharField(max_length=256)
address = models.CharField(max_length=1024)
city = models.CharField(max_length=256)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
email = models.EmailField(max_length=256, unique=True)
phone = models.CharField(max_length=256)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
integration = models.ForeignKey(Integration, null=True, on_delete=models.SET_NULL)

def __str__(self):
return self.first_name


class Company(models.Model):
name = models.CharField(max_length=256)
address = models.CharField(max_length=256)
city = models.CharField(max_length=256)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)

def __str__(self):
return self.name


class ContactCompany(models.Model):
contact = models.ForeignKey(Contact, on_delete=models.CASCADE, related_name='job')
company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='company')
job = models.TextField(blank=True, help_text='Job', max_length=5000, null=True)
started_at = models.DateTimeField(auto_now_add=True)
finished_at = models.DateTimeField(auto_now=True)

def __str__(self):
return self.job

这是我的序列化器

class ContactSerializer(serializers.ModelSerializer):

country_name = serializers.CharField(source='country.name')
category_name = serializers.CharField(source='category.name')
user_name = serializers.CharField(source='user.email')
integration_name = serializers.CharField(source='integration.name')

class Meta:
model = Contact
fields = ('first_name', 'last_name', 'address', 'city', 'country_name', 'email', 'phone',
'category_name', 'user_name', 'integration_name', 'job', )

如何从此序列化程序中的 ContactCompany 模型访问公司字段?

API 响应如下所示

{
"first_name": "First name",
"last_name": "Last name",
"address": "Address",
"city": "City",
"country_name": "Gabon",
"email": "saidsadiasida@gmam.com",
"phone": "0712345678",
"category_name": "TestCategory",
"user_name": "ekartdragos@cyberaxo.com",
"integration_name": "testmonth",
"job": [
4
]
}

如何显示作业文本而不是 ID?

最佳答案

一种可能的解决方案是使用 serializerMethodField并获取您想要的信息。

class ContactSerializer(serializers.ModelSerializer):

country_name = serializers.CharField(source='country.name')
category_name = serializers.CharField(source='category.name')
user_name = serializers.CharField(source='user.email')
integration_name = serializers.CharField(source='integration.name')
company = serializers.SerializerMethodField()

class Meta:
model = Contact
fields = ('first_name', 'last_name', 'address', 'city', 'country_name', 'email', 'phone',
'category_name', 'user_name', 'integration_name', 'job', 'company' )
def get_company(self, obj):
"""
at first we get the instance of ContactCompany from the id of object. then get the company information
"""
try:
contact_company = ContactCompany.objects.get(contact=obj.id)
expect ContactCompany.DoesNotExist:
# return or raise your desire this

return contact_company.company # here we have company id of this contact, also we can generate all company information from this.


关于python - 如何从序列化器中的另一个模型访问外键字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58767709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com