擅长:python、mysql、java
<p>在模型定义<a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#unique" rel="noreferrer">you can tell Django that 'name' should be unique</a>中:</p>
<pre><code>name = models.CharField(max_length=8, unique=True)
</code></pre>
<p>如果试图用相同的名称保存两条记录,将引发<code>django.db.IntegrityError</code>。</p>