gpt4 book ai didi

json - Django Rest Framework - M2M 通过

转载 作者:行者123 更新时间:2023-12-02 01:21:33 24 4
gpt4 key购买 nike

我无法从序列化程序中检索我需要的所有字段。场景:

我有 2 个表:项目和仓库。这些表通过名为 InventoryStatus 的表通过 M2M 关系连接。我需要的是从 InventoryStatus 表中获取带有嵌套仓库信息 + 状态字段的项目信息。问题是,我无法获取“状态”字段....

型号:

class InventoryItem(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
code = models.CharField(max_length=25, null=False, blank=True)
orgid = models.ForeignKey('glonass.Company', null=True, related_name='inventory_items_company')
title = models.CharField(max_length=150, null=False, blank=True)
inventory = models.ManyToManyField('CompanyBranch', through='InventoryStatus', related_name='branch_iventoryStatus')

class CompanyBranch(BaseDataDescTime, LocationInfo, PostInfo, PoboxInfo, ContactInfo):
id = models.UUIDField(primary_key=True, default=uuid.uuid4,
orgid = models.ForeignKey('glonass.Company', null=True, related_name='branches')
slug = models.SlugField(max_length=150, blank=True, null=False)

class InventoryStatus(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
inventoryitem = models.ForeignKey('InventoryItem', related_name='inventory_status_items', null=True)
orgid = models.ForeignKey('Company', null=True, related_name='inventory_status_company')
companybranch = models.ForeignKey('CompanyBranch', related_name='inventory_status_warehouses', null=True)
status = models.DecimalField(max_digits=9, decimal_places=2, null=True, blank=True, default=0)

序列化器:

class InventoryStatusSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = InventoryStatus

class CompanyBranchSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = CompanyBranch

class InventoryItemSerializer(serializers.HyperlinkedModelSerializer):
inventory = CompanyBranchSerializer(many=True)

class Meta:
model = InventoryItem

需要的输出:

[
{
'id': 'xxxxxxxxxxxxx',
'title': 'Car',
'inventory': [
{
'id': 'yyyyyyyyyyy',
'title': 'warehouse 1',
'status': 50
},
{
'id': 'zzzzzzzzzzzz',
'title': 'warehouse 2',
'status': 10
}
]
}
]

最佳答案

问题是 M2M 关系直接给出相关对象,而没有任何关于“through”表的信息,当你想序列化并获取“through”表中的一些信息时,这会很痛苦。为了解决这个问题,我通常使用“through”关系从“through”表和相关项中抓取信息。

在您的情况下,它可能类似于:

class InventoryStatusSerializer(serializers.ModelSerializer):
companybranch = CompanyBranchSerializer(many=True, read_only=True)
class Meta:
model = InventoryStatus
fields = ('status', 'companybranch')

class InventoryItemSerializer(serializers.ModelSerializer):
inventory = InventoryStatusSerializer(source='inventory_status_items', many=True, read_only=True)

class Meta:
model = InventoryItem

这会给出类似的输出(注意:我认为你的意思是“slug”,而不是你示例的库存项目中的“title”):

[
{
'id': 'xxxxxxxxxxxxx',
'title': 'Car',
'inventory': [
{
'status': 50
'companybranch': {
'slug': 'warehouse 1',
'id': 'yyyyyyyyyyy', }
},
{
'status': 10
'companybranch': {
'slug': 'warehouse 2',
'id': 'zzzzzzzzzzzz',
}
}
]
}
]

现在,如果您想要展平输出,只需更改 InventoryStatusSerializer:

class InventoryStatusSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField(source='companybranch.id')
slug = serializers.ReadOnlyField(source='companybranch.slug')
class Meta:
model = InventoryStatus
fields = ('status', 'id', 'slug')

关于json - Django Rest Framework - M2M 通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39964765/

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