我無法從串行器中檢索所需的所有字段。 場景:Django Rest框架 - M2M通過
我有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
}
]
}
]
,你會得到什麼樣的智慧你的序列號?沒有「狀態」字段的所有內容? –
是...當我用 庫存= InventoryStatusSerializer(多=真) 我得到CompanyBranch ID代替ID已從InventoryStatus表...即使當我有InventoryStatus觀點......在InventoryStatus視圖我可以得到場像從任何其他表...但我不能將此結果嵌入到項目的結果 –