Hid*_*vis 1 python django foreign-key-relationship
我是Django的新手,我想实现以下目标:我有两个表之间的关系,比如表B有一个表A的ManyToMany引用.现在我想要一个名为Options的表,它将选项保存到A&之间的特定组合B.我如何实现这一目标?
谢谢!
Hidde
使用Field 的through选项ManyToMany,并在关系本身中添加信息.
例如
class Ingredient(models.Model):
name = models.TextField()
class Recipe(models.Model):
name = models.TextField()
ingredients = models.ManyToManyField(Ingredient, through='RecipePart')
class RecipePart(models.Model)
recipe = models.ForeignKey(Recipe)
ingredient = models.ForeignKey(Ingredient)
amount = models.IntegerField()
# ...
RecipePart(recipe=pizza, ingredient=cheese, amount=9001).save()
Run Code Online (Sandbox Code Playgroud)
如果关系已经存在(并且您已经拥有数据),则必须更新数据库模式(如果您曾经使用自动映射,则创建模型).南可以帮助你做到这一点.
| 归档时间: |
|
| 查看次数: |
870 次 |
| 最近记录: |