0
模型對象,我有以下模型 -創建了M2M和FK關係
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
和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')
我再「得到」這四個對象 -
>>> cat1 = Category.objects.get(category='animation')
>>> cat2 = Category.objects.get(category='drama')
>>> cred1 = Credit.objects.get(person='john')
>>> cred2 = Credit.objects.get(person='tom')
但我在創建時遇到困難對象。我將如何創建以下兩個對象?
1) Video.objects.create(title='little italy', category=cat1, credits=cred1) ??
2) Video.objects.create(title='new film', category=cat2, credits=cred1, cred2) ??
謝謝。
嘗試像[這裏](https://docs.djangoproject.com/en/dev/topics/db/queries/#saving-foreignkey-and- manytomanyfield場)。 – 2011-05-29 21:50:42