开发者

Update django foreign key field

I have a foreign key field called books (in the model Book) in an intermediate model called Link_Book_Courses.

I'd like to add multiple Book objects to thi开发者_StackOverflows. How do I do that in django?


Related Objects have an add() method:

https://docs.djangoproject.com/en/1.3/ref/models/relations/#django.db.models.fields.related.RelatedManager.add

You can add several like this

mymodel.related.add(*OtherModel.objects.filter(...))


For one ForeignKey you can only have one book. So to add multiple entries, you’ll have to add multiple Link_Book_Courses-objects. However what you might really want is a ManyToManyField.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜