Python Django:两个字段是唯一的,但仍然无法满足唯一约束

Python Django:两个字段是唯一的,但仍然无法满足唯一约束,python,django,unit-testing,testing,django-models,Python,Django,Unit Testing,Testing,Django Models,我正在写一些测试来检查我基本博客应用程序的模型。该模型要求博客标题是唯一的。以下是我为保存两篇博客文章而编写的测试的主体: first_post.title = "First Post!" first_post.body = "This is the body of the first post" first_post.pub_date = datetime.date.today() first_post.tags = all_tags[0] first_

我正在写一些测试来检查我基本博客应用程序的模型。该模型要求博客标题是唯一的。以下是我为保存两篇博客文章而编写的测试的主体:

    first_post.title = "First Post!"
    first_post.body = "This is the body of the first post"
    first_post.pub_date = datetime.date.today()
    first_post.tags = all_tags[0]
    first_post.slug = "first_post"
    first_post.save()


    second_post = Post()
    second_post.title = "Second Post!"
    self.assertNotEqual(first_post.title,second_post.title)
    second_post.body = "This is the body of the Second post"
    second_post.pub_date = datetime.date.today()
    second_post.tags = all_tags[1]
    second_post.slug = "second"
    second_post.save()
注意
self.assertNotEqual(第一个\u post.title,第二个\u post.title)
。我添加这一点是因为当我运行测试时,我不断得到
django.db.utils.integrityyror:UNIQUE constraint失败:blog\u post.title\u text
。当我看完用这个吐出来的vomitext的其余部分时,它指向
second\u post.save()
。但是,
assertNotEqual
始终通过,如果我将其更改为
assertEqual
则失败

无论我在标题值中输入什么,我都会得到相同的错误。为什么这两个Post对象被认为具有相同的标题

以下是博客模型供参考:

class  Post(models.Model):
    title_text = models.CharField(max_length = 200, unique = True)
    pub_date = models.DateTimeField('date published')
    post_tags = models.ManyToManyField('Tag')
    post_body = models.TextField()
    slug = models.SlugField(max_length = 50, unique = True)

模型中的字段名为
title\u text
,但在测试中使用
title
。因此,在这两种情况下,
title\u text
的db值都将为“”

更改为:

first_post.title_text = "First Post!"
first_post.body = "This is the body of the first post"
first_post.pub_date = datetime.date.today()
first_post.tags = all_tags[0]
first_post.slug = "first_post"
first_post.save()


second_post = Post()
second_post.title_text = "Second Post!"
self.assertNotEqual(first_post.title_text,second_post.title_text)
second_post.body = "This is the body of the Second post"
second_post.pub_date = datetime.date.today()
second_post.tags = all_tags[1]
second_post.slug = "second"
second_post.save()

伙计,我需要更多的咖啡。好地方!谢谢一旦时间允许,我会接受的。