django模型可以具有唯一且共享的非唯一字段吗?

时间:2022-07-12 18:17:57

I have a model with a CharField with max_length=16 called verify_key:

我有一个模型,其中CharField的max_length = 16,名为verify_key:

verify_key = models.CharField(unique=True, max_length=16) 

The purpose of this field is to be a unique, 16 character alphanumeric identifier. When a user registers, they are sent this code, they return this code and their verified status is set to true.

该字段的目的是成为唯一的16个字符的字母数字标识符。当用户注册时,他们会发送此代码,他们返回此代码并将其验证状态设置为true。

In the unlikely event that two users somehow manage to generate the same key, the latest won't be able to register. What I would like to do when a user registers is set their verify_key to be an empty string. I can't do this with the unique parameter. How do I fix this or is there a better method?

万一两个用户以某种方式设法生成相同的密钥,最新的将无法注册。当用户注册时我想要做的是将其verify_key设置为空字符串。我不能用unique参数做到这一点。我该如何解决这个问题还是有更好的方法?

4 个解决方案

#1


0  

You can set null=True on the field and set the field to None. A column with a UNIQUE constraint can have multiple NULL values.

您可以在字段上设置null = True并将字段设置为None。具有UNIQUE约束的列可以具有多个NULL值。

You'll have to be explicit when using forms or default values. The default for a CharField is an empty string, and empty form values will also be saved as an empty string. In both cases you need to explicitly set the value to None instead.

使用表单或默认值时,您必须明确。 CharField的默认值为空字符串,空表单值也将保存为空字符串。在这两种情况下,您都需要将值显式设置为None。

#2


1  

import uuid   
verify_key = models.CharField(max_length=50,
                              unique=True,
                              default=uuid.uuid4)

If you are using django 1.8 you also have UUIDField. Here's Django doc.

如果你使用的是django 1.8,你也有UUIDField。这是Django doc。

#3


0  

You can't do this in your model class.

您不能在模型类中执行此操作。

You just need to add null=True to the field and set Model.verify_key=None whenever you create an instance. Also, you can define a custom model form to deal with this in the forms.

您只需要在字段中添加null = True,并在创建实例时设置Model.verify_key = None。此外,您可以定义自定义模型表单以在表单中处理此问题。

#4


0  

You can't set an empty string, but you can set the value to NULL. NULL is not equals to NULL when Django check for uniqueness.

您不能设置空字符串,但可以将值设置为NULL。当Django检查唯一性时,NULL不等于NULL。

#1


0  

You can set null=True on the field and set the field to None. A column with a UNIQUE constraint can have multiple NULL values.

您可以在字段上设置null = True并将字段设置为None。具有UNIQUE约束的列可以具有多个NULL值。

You'll have to be explicit when using forms or default values. The default for a CharField is an empty string, and empty form values will also be saved as an empty string. In both cases you need to explicitly set the value to None instead.

使用表单或默认值时,您必须明确。 CharField的默认值为空字符串,空表单值也将保存为空字符串。在这两种情况下,您都需要将值显式设置为None。

#2


1  

import uuid   
verify_key = models.CharField(max_length=50,
                              unique=True,
                              default=uuid.uuid4)

If you are using django 1.8 you also have UUIDField. Here's Django doc.

如果你使用的是django 1.8,你也有UUIDField。这是Django doc。

#3


0  

You can't do this in your model class.

您不能在模型类中执行此操作。

You just need to add null=True to the field and set Model.verify_key=None whenever you create an instance. Also, you can define a custom model form to deal with this in the forms.

您只需要在字段中添加null = True,并在创建实例时设置Model.verify_key = None。此外,您可以定义自定义模型表单以在表单中处理此问题。

#4


0  

You can't set an empty string, but you can set the value to NULL. NULL is not equals to NULL when Django check for uniqueness.

您不能设置空字符串,但可以将值设置为NULL。当Django检查唯一性时,NULL不等于NULL。