简体   繁体   English

删除用户会影响使用匹配结果计数的增量唯一引用。 如何保持增量?

[英]Deleting user affects incremental unique reference which uses count of matching results. How to keep increment?

For a variety of reasons I might need to delete a user.由于多种原因,我可能需要删除用户。 A soft delete in which I simply delete all identifiable info and leaving some anonymised might not be an option.我只是删除所有可识别信息并留下一些匿名信息的软删除可能不是一种选择。 Additionally, if I delete the user inside the django admin site then it would be a hard delete.此外,如果我删除 django 管理站点内的用户,那么这将是一个硬删除。

The issue is that I have a model for the user which includes first_name, last_name, company_name, reference_id etc. The reference_id is created by using first 3 consonants of the last name, first 3 letters of the first name and then an incremental 3 digit number starting with 001. If any of the names have less than 3 letters the missing letters are replaced with an 'X'.问题是我有一个 model 供用户使用,其中包括 first_name、last_name、company_name、reference_id 等。reference_id 是通过使用姓氏的前 3 个辅音、名字的前 3 个字母然后是递增的 3 位数字创建的以 001 开头。如果任何名称的字母少于 3 个,则缺失的字母将替换为“X”。 The numeric part uses a model lookup with first 6 letters, counting the number of results.数字部分使用前 6 个字母的 model 查找,计算结果的数量。 incrementing by 1 and using zfill to fill the missing number of digits.递增 1 并使用 zfill 填充缺失的位数。 A full reference_id is like this: MDVTAS001 for example:完整的 reference_id 如下所示: MDVTAS001 例如:

first_name_tres = first_name.lower()[0:3].ljust(3,'x')
print("first name 3: " + first_name_tres)
last_name_tres = last_name.lower().translate({ord(i): None for i in 'aeiou'})[0:3].ljust(3,'x')
print("last name 3: " + last_name_tres)
user_ref_count =  str(ProfileMember.objects.filter(reference_id__contains=(last_name_tres + first_name_tres)).count()+1).zfill(3)
print("user ref count: " + user_ref_count)
reference_id = last_name_tres + first_name_tres + user_ref_co

The issue with this method is that if for example is 2 users ie.这种方法的问题是,例如,如果是 2 个用户,即。 MDVTAS001 and MDVTAS002 and I delete MDVTAS001 then the count is 1 and the next matching user will get MDVTAS002 but that already exists and since I am setting this value to be unique it will fail. MDVTAS001 和 MDVTAS002,然后我删除 MDVTAS001,然后计数为 1,下一个匹配的用户将获得 MDVTAS002,但它已经存在,并且由于我将此值设置为唯一,因此它将失败。 Now if have 5 users and 2 are deleted the issue is compounded further.现在,如果有 5 个用户和 2 个被删除,问题会更加复杂。

How can I ensure that all the value is always incrementing higher even if users have been deleted?即使用户已被删除,如何确保所有值始终递增?

last_record = ProfileMember.objects.filter(reference_id__startswith=(last_name_tres + first_name_tres)).order_by('reference_id').last()
if last_record:
    reference_id = last_name_tres + first_name_tres + str(int(last_record.reference_id[-3:]) + 1).zfill(3)
else:
    reference_id = last_name_tres + first_name_tres + '001'

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

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