[英]In django how to get the all the data from table one with the related data from the table two in a single context
我的models.py中有兩個類,它們是item和batch 。 我需要從 class批處理中獲取項目的所有數據以及適當的 batch_id 。 我應該如何編寫views.py以將所有數據放到一個上下文中
class item(models.Model):
item_name=models.CharField(max_length=100)
item_code=models.IntegerField()
min_stock=models.IntegerField()
current_stock=models.IntegerField()
def __str__(self):
return self.item_name
class batch(models.Model):
batch_id=models.IntegerField()
item_name=models.CharField(max_length=100)
item_code=models.IntegerField()
def __str__(self):
return self.item_name
您應該將 ForeignKey 從“item”設置為“batch”表:
class item(models.Model):
batch = models.ForeignKey(batch, on_delete=models.CASCADE)
你的意見.py:
class itemViewSet(viewsets.ModelViewSet):
queryset = item.objects.all()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.