交互式 shell 中忽略 Django 唯一字段
我定义了一个名为 Country 的类,它具有唯一的名称字段。
class Country(models.Model):
class Meta:
verbose_name_plural = "Countries"
name = models.CharField(max_length=100, unique=True, null=False)
def __unicode__(self):
return self.name
在管理页面上,这的行为正如我所期望的那样。创建数据库中已存在的国家/地区失败,并显示错误“具有此名称的国家/地区已存在。”。完美的。
当我尝试在交互式提示符(manage.py shell
)中测试相同的内容时,没有给出此类错误。相反,重复的对象只是添加到数据库中。
>>> from rack.models import Country
>>> usa = Country(name="United States of America")
>>> usa.save()
>>> canada = Country(name="United States of America")
>>> canada.save()
>>> canada.name
'United States of America'
>>> Country.objects.all()
[<Country: United States of America>, <Country: United States of America>]
我对 Django 很陌生,任何人都可以启发我为什么 shell 会忽略 unique 字段吗?
I've defined a class called Country that has a unique name field.
class Country(models.Model):
class Meta:
verbose_name_plural = "Countries"
name = models.CharField(max_length=100, unique=True, null=False)
def __unicode__(self):
return self.name
On the admin page, this behaves as I'd expect it to. Creating a country that is already in the database fails with the error "Country with this Name already exists.". Perfect.
When I try to test the same thing in the interactive prompt (manage.py shell
), no such error is given. Instead the duplicate object is just added to the database.
>>> from rack.models import Country
>>> usa = Country(name="United States of America")
>>> usa.save()
>>> canada = Country(name="United States of America")
>>> canada.save()
>>> canada.name
'United States of America'
>>> Country.objects.all()
[<Country: United States of America>, <Country: United States of America>]
I'm quite new to Django, can anyone enlighten me as to why the shell ignores the unique field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你重置你的数据库表了吗?我的猜测是您之前定义的模型没有 unique=True。
文档说 unique 是在管理级别和数据库级别强制执行的,这与您的症状相符!也就是说......它在管理中工作,而不是在外壳中工作。
http://docs.djangoproject。 com/en/dev/ref/models/fields/#django.db.models.Field.unique
Have you reset your DB table? My guess is that you defined the model previously without unique=True.
The docs say that unique is enforced at the admin level and the database level, which matches your symptoms! That is.. it works in admin, not in shell.
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.unique
你使用什么数据库?如果您使用 sqlite 和 South,则存在一个错误,它不允许添加唯一约束,因此sqlite 表。
如果是这种情况,可能是管理表单强制执行唯一性,因此检查甚至永远不会到达数据库,但是当您从命令行执行此操作时,它依赖数据库来强制执行唯一性(sqlite 不这样做)在这种情况下执行)。
What database are you using? If you are using sqlite and South, there is a bug which doesn't allow adding unique constraints so sqlite tables.
If that's the case, it may be that the admin form enforces the uniqueness, so the check never even gets to the database, but when you do it from the command line, it's relying on the database to enforce uniqueness (which sqlite doesn't do in this case).
您确定从
rack.models
加载的Country
使用的是您认为的版本吗?我会检查它的 __file__ 并确保它没有使用它的某些缓存版本。或者,如果您在导入模型后对其进行修改,则不一定会出现这种情况。unique
应该在模型层和数据库层强制执行唯一性Are you sure the
Country
you're loading fromrack.models
is using the version you think it is? I'd check the__file__
on it and make sure it's not using some cached version of it. Or if you modify the model after it's imported it wouldn't necessarily trip that.unique
should enforce uniqueness at both the model and the database layer