这是我的模特:
class Fruit(models.Model):
name = models.CharField(max_length=5)
number = models.IntegerField()
class BoxContent(models.Model):
fruit = models.ForeignKey(Fruit)
class Box(models.Model):
number = models.CharField(max_length=11)
myitems = models.ManyToManyField(BoxContent, blank=True, null=True)
和我的观点:
box = Box.objects.create(number="test")
box.myitems.add(BoxContent.objects.create(fruit=...)
但是如何创建Fruit
对象?
答案 0 :(得分:0)
水果创作不应与其他模型对象创作有任何不同
fruit = Fruit.objects.create(name='orange', number=1)
box_content = BoxContent.objects.create(fruit=fruit)
box = Box.objects.create(number="test")
box.myitems.add(box_content)