class MyArticle(models.Model):
id = models.CharField(max_length=150, primary_key=True)
def __unicode__(self):
return self.id
class UserProfile(models.Model):
username = models.CharField(max_length=765)
clicks = models.ManyToManyField(MyArticle, through='ClickEvent')
def __unicode__(self):
return self.username
class ClickEvent(models.Model):
article = models.ForeignKey(MyArticle)
user = models.ForeignKey(UserProfile)
timestamp = models.DateTimeField()
art = MyArticle.objects.create(id = article_id)
eusr = UserProfile.objects.create(username = username)
cl = ClickEvent(article = art, user = eusr, timestamp = datetime.now())
cl.save()
这些对象都已创建,但是当我尝试保存单击时它失败了,我无法理解为什么?
cl.save()#失败
我按照这里描述的步骤,但似乎无法看到缺少的内容。 http://docs.djangoproject.com/en/dev/topics/db/models/#many-to-many-relationships
答案 0 :(得分:0)
您是否曾尝试首先保存艺术品和eusr项目 - 例如:
art.save()
eusr.save()
在调用cl.save()之前