class Box(models.Model):
owner = models.ForeignKey(User, related_name='user', verbose_name='Owner')
name = models.CharField("Name", max_length=30)
items = models.ManyToManyField('Item', through='BoxItem', blank=True, null=True)
class BoxItem(models.Model):
item = models.ForeignKey('Item')
box = models.ForeignKey('Box')
quantity = models.IntegerField()
class Item(models.Model):
name = models.CharField("Name Item", max_length=30)
In [1]: from project.app.models import *
In [2]: b = Box.objects.get(owner=1)
In [3]: b.items_all()
Out[4]: [<Item: PC>, <Item: Notebook>]
- 如何将此输出转换为列表?
- 如何在of
quantity
旁边添加到此列表?(例如:PC - 3,笔记本电脑 - 5)name
Item