如何在Django中设置唯一字段? [英] How can I set the field unique in django?

查看:72
本文介绍了如何在Django中设置唯一字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型课:

class PysicalServer(models.Model):

    serial_number = models.CharField(max_length=64)  # I want to add the unique
    name = models.CharField(max_length=16)

我知道使用primary_key可以设置唯一,但是serial_number不是我的id字段,我不能使用primary_key,还有其他可以设置字段唯一的属性吗?

I know use the primary_key can set unique, but the serial_number is not my id field, I can not use the primary_key, is there other property I can set field unique?

推荐答案

只需添加 unique = True serial_number = models.CharField(max_length = 64,unique = True)在此处查看更多信息 https://docs.djangoproject.com/en/1.11/ref/models/fields/#unique

just add unique=True serial_number = models.CharField(max_length=64, unique=True) see more information here https://docs.djangoproject.com/en/1.11/ref/models/fields/#unique

这篇关于如何在Django中设置唯一字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆