0

那是我的models.py:

class User(models.Model):
    email = models.EmailField()

    def __unicode__(self):
        return str(self.email)


class Link(models.Model):
    link = models.URLField()
    users = models.ManyToManyField(User, through='ShortURL')


class ShortURL(models.Model):
    link = models.ForeignKey(Link)
    user = models.ForeignKey(User)
    short_end = models.CharField(max_length=20)
    counter = models.IntegerField() 

添加用户工作得很好:

>>> user = User.objects.create("john_doe@planetearth.com")

尝试添加链接时出现完整性错误:

>>> link = Link.objects.create("stackoverflow.com")
IntegrityError: shortener_link.short_end may not be NULL

我错过了什么?

4

1 回答 1

0

您需要创建这样的链接:

Link.objects.create(link="stackoverflow.com", user = request.user, short_end = 'stack', counter = 0)

因为所有这些字段都是必需的。

于 2012-07-07T01:19:37.833 回答