本文介绍了创建具有M2M和FK关系的模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下模型 -class Category(models.Model): category = models.CharField(max_length=100)class Credit(models.Model): person = models.CharField(max_length=100) position = models.CharField(max_length=100)class Video(models.Model): title = models.CharField(max_length=100) category = models.ForeignKey(Category) credits = models.ManyToManyField(Credit) 和信用对象 ->>> Category.objects.create(category='animation')>>> Category.objects.create(category='drama')>>> Credit.objects.create(person='john', position='director')>>> Credit.objects.create(person='tom', position='writer')获得'这四个对象 -I then 'get' those four objects -->>> cat1 = Category.objects.get(category='animation')>>> cat2 = Category.objects.get(category='drama')>>> cred1 = Credit.objects.get(person='john')>>> cred2 = Credit.objects.get(person='tom')但是我无法创建视频对象。如何创建以下两个对象?But I'm having difficulty creating the Video object. How would I create the following two objects?1) Video.objects.create(title='little italy', category=cat1, credits=cred1) ??2) Video.objects.create(title='new film', category=cat2, credits=cred1, cred2) ??谢谢。推荐答案>>> v1 = Video.objects.create(title='little italy', category=cat1)>>> v1<Video: little italy>>>> v1.credits.add(cred1) ...等... 这篇关于创建具有M2M和FK关系的模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 08:01