开发者

Update django foreign key field

开发者 https://www.devze.com 2023-04-04 07:52 出处:网络
I have a foreign key field called books (in the model Book) in an intermediate model called Link_Book_Courses.

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

精彩评论

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