gpt4 book ai didi

python - 我想获取 Django Rest Framework 中外键的 url

转载 作者:行者123 更新时间:2023-12-01 09:23:06 25 4
gpt4 key购买 nike

我有一个带有 ForeignKey 的模型的 HyperlinkedModelSerializer,我想返回指向该字段中引用的实例的超链接,但我得到了嵌套的整个对象在 json 中。

这些是我的模型:

class Hotel(models.Model):

ONE_STAR = '*'
TWO_STARS = '**'
THREE_STARS = '***'
FOUR_STARS = '****'
FIVE_STARS = '****'
GRAND_TOURISM = 'GRAND_TOURISM'
NA = 'NA'
SPECIAL = 'SPECIAL'
ECO = 'ECO'
BOUTIQUE = 'BOUTIQUE'

HOTEL_CATEGORY_CHOICES = (
(ONE_STAR, _('*')),
(TWO_STARS, _('**')),
(THREE_STARS, _('***')),
(FOUR_STARS, _('****')),
(FIVE_STARS, _('*****')),
(GRAND_TOURISM, _('Grand Tourism')),
(NA, _('NA')),
(SPECIAL, _('Special')),
(ECO, _('Eco-Hotel')),
(BOUTIQUE, _('Boutique-Hotel'))
)

company = models.OneToOneField(Company, on_delete=models.CASCADE, primary_key=True, verbose_name=_('Company'))
code = models.CharField(max_length=10, verbose_name=_('Code'))
city = models.ForeignKey(City, on_delete=models.PROTECT, related_name='hotels', verbose_name=_('City'))
category = models.CharField(max_length=20, choices=HOTEL_CATEGORY_CHOICES, verbose_name=_('Category'))
capacity = models.IntegerField(verbose_name=_('Capacity'))
position = models.DecimalField(max_digits=11, decimal_places=2, default=0.00, verbose_name=_('Position'))
in_pickup = models.BooleanField(default=False, verbose_name=_('In pickup?'))
is_active = models.BooleanField(default=True, verbose_name=_('Is active?'))

class Meta:
verbose_name = _('Hotel')
verbose_name_plural = _('Hotels')

def __str__(self):
return self.company.name

class Company(models.Model):
name = models.CharField(max_length=40, verbose_name=_('Name'))
legal_name = models.CharField(max_length=100, null=True, blank=True, verbose_name=_('Legal name'))
tax_id = models.CharField(max_length=12, null=True, blank=True, verbose_name=_('Tax ID'))
url = models.URLField(null=True, blank=True, verbose_name=_('URL'))
address = models.TextField(max_length=400, null=True, blank=True, verbose_name=_('Address'))

class Meta:
verbose_name = _('Company')
verbose_name_plural = _('Companies')

def __str__(self):
return "[{}]{}".format(self.id, self.name)

这是我的序列化器:

class HotelProductsSerializer(serializers.HyperlinkedModelSerializer):
company = CompanySerializer()
products = serializers.SerializerMethodField()

def get_products(self, instance):
queryset = [product for product in instance.company.products.all()]
return ProductSerializer(queryset, many=True, context=self.context).data

class Meta:
model = models.Hotel
fields = ('company', 'products')

这是我的 View 集:

class HotelProductsViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticated,)
queryset = models.Hotel.objects.all()
serializer_class = serializers.HotelProductsSerializer

一切正常,但字段 company 会生成一个嵌套的 Company 对象,而我希望获得指向该对象的超链接。

感谢您的帮助。

最佳答案

company 字段被序列化为嵌套对象,因为您已使用嵌套序列化程序声明了它。尝试更换线路

company = CompanySerializer()

像这样的东西

company = serializers.HyperlinkedRelatedField(
many=False,
read_only=True
)

您可能需要根据 the docs 调整选项.

关于python - 我想获取 Django Rest Framework 中外键的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50651509/

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