简体   繁体   English

Django Python unique =真表达式

[英]Django Python unique=True Expression

I want to save Entries in my database such that I can delete them later. 我想将条目保存在数据库中,以便以后可以删除它们。 To identify them, i put the key attribute in my class Entry in models.py as shown below: 为了识别它们,我将key属性放在models.pyEntry中,如下所示:

class Entry(models.Model):
    name = models.CharField(max_length=200)
    key = models.IntegerField(unique=True,default=0)

Every time I start the server, I will find the biggest key that is in Entry.objects.all() . 每次启动服务器时,我都会在Entry.objects.all()找到最大的密钥。

Every time I create the first new Entry after starting the server, I want to take the key I found in the very beginning, increase it by 1 and then set this as the key for my new Entry . 每次启动服务器后,每次创建第一个新Entry ,我都想使用在开始时找到的key ,将其增加1,然后将其设置为新Entrykey For every subsequent Entry , I will just take the key of the previous element, increase it by 1 and. 对于随后的每个Entry ,我都将采用上一个元素的key ,将其增加1并添加。 set it as the key for my new Entry 将其设置为新Entrykey

However, before I do this, I want to know what Django considers as unique. 但是,在执行此操作之前,我想知道Django认为什么是独特的。 For example, if i added three entries initially with keys 1 , 2 and 3 , and then I delete the last element with key 3 . 例如,如果我增加三个条目最初与键123 ,然后我删除与键的最后一个元素3 If I then restart the server, the biggest key I will find is 2 and the next Entry I will add will have key 2+1 which is equal to 3 again. 如果我随后重新启动服务器,我将找到的最大key2 ,而我要添加的下一个Entry将具有key 2 + 1,该key再次等于3

Is this key unique? 这把key唯一吗? It was entered before but I deleted that element right? 它是在之前输入的,但是我删除了该元素,对吗? So is uniqueness determined by whatever I entered in the past or just depending on the elements currently in the database? 那么,唯一性是由我过去输入的内容还是仅取决于数据库中当前的元素确定的?

Also, does Django keep a track of all the instances of Entry that I added, so that it would somehow know if I added an Entry with key 3 before? 另外,Django是否会跟踪我添加的Entry的所有实例,以便以某种方式知道我之前是否添加了带有key 3Entry

Note that my data is stored in a file called db.sqlite3 请注意,我的数据存储在名为db.sqlite3的文件中

Thank you. 谢谢。

Seems like you are looking for something that already exists; 似乎您正在寻找已经存在的东西; models have an id field by default which is unique and monotonic (newer entries have bigger id). 模型默认具有唯一且单调的id字段(更新的条目具有更大的id)。

尝试在您的键字段中添加primary_key=True

None of this has anything to do with Django at all. 这些都与Django无关。 This is pure database stuff: unique constraints are handled exclusively by the database, in your case SQLite, although the functionality is exactly the same for other DBs as well. 这是纯数据库的东西:唯一的约束仅由数据库(在您的情况下为SQLite)处理,尽管其他DB的功能也完全相同。

Of course, a unique constraint only takes into account rows that actually exist: what would be the point otherwise? 当然,唯一约束仅考虑实际存在的行:否则有什么意义?

There are other things to bear in mind with your proposal as well. 您的建议还需要牢记其他事项。 In particular, there is the issue of race conditions: if you do a query to get the maximum key value, and then create a new row with key+1, you run the risk of another user in the meantime adding their own row at key+1. 特别是存在竞争条件的问题:如果执行查询以获取最大键值,然后使用key + 1创建新行,则可能会冒另一个用户同时在key处添加自己的行的风险。 +1。 Much better, as Iris and ryanageles suggest, to use the built-in primary key which is already automatically defined as the id field. 正如Iris和ryanageles建议的那样,最好使用已经自动定义为id字段的内置主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM