#Unique field problem

61 messages ยท Page 1 of 1 (latest)

vale hawk
#

I have a Profile model where I have this unique field

referral_code = models.CharField(max_length=200, unique=True)

I have a generate_ref_code() that generate a code.
How can I make sure that this code is unique for the field?

new_ref_code = generate_ref_code()
Profile.objects.create(user=instance, referral_code=new_ref_code)

I can't do like

try:
   new_ref_code = generate_ref_code()
   Profile.objects.create(user=instance, referral_code=new_ref_code)
except IntegityError:
    # generate a new code and try again

Because
IntegrityError exception only occur when try to save it

Profile.objects.create(user=instance, referral_code=new_ref_code).save() 

Therefore how can I check this uniqueness of the code.

crude rose
#

You would need to show us generate_ref_code() before we can help

vale hawk
crude rose
#

why not just use python's build in uuid or secrets?

#

def generate_ref_code():
return uuid.uuid4```
#

that's what I generally use for my referral / invite codes, and I have yet to have it duplicate one

vale hawk
crude rose
#

that I don't know, I know you can with secrets

#

but, if you're having issues with uniquity, longer would be better for you

#

also, copyright Thalimet 2023, Uniquity

#

lol

vale hawk
crude rose
#

that's an expensive, and flawed way of doing it

#

the more codes that are generated, the longer that is likely to take

vale hawk
crude rose
#

one sec

vale hawk
crude rose
#

lol, tell the marketing people to leave the technical decisions to the people implementing them ๐Ÿ˜›

#

(kidding, mostly)

#

def generate_ref_code():
  return secrets.token_hex(3)```
#

give that a try

#

you can check that against the database if you want, but using the token hex function of secrets should generate a pretty unique code

vale hawk
#

How can I check it against the data base

crude rose
#

I'd say you already have the logic built in

#

if there's an integrity error on that field, generate a new one and try again

vale hawk
#

What's the better way to check against the database

crude rose
#

16,777,216 - btw this is the max possible combinations that can exist for a six character string like that

vale hawk
crude rose
#

ok, so, do you know how to filter?

#

you could take the generated code, filter for objects that have that code as the relevant attribute

#

if any exist in the queryset, then you can generate another code

vale hawk
# crude rose ok, so, do you know how to filter?

Wait I can do something with objects.get() because it returns only one unique objects. If there is two objects then it raise exception, so if return object then not unique, if Doesn'tExist exception then the code is unique otherwise if the exception is different then multiple exists

crude rose
#

it's easier with filter

crude rose
#

because you can do objects.filter(attribute=blah).exists()

#

and it returns a true/false value

vale hawk
#

Mmm . exists better then checking multiple exceptions

crude rose
#

multiples can't exist under your schema

#

so it wouldn't matter ๐Ÿ˜„

vale hawk
#

Thanks I will implement this

crude rose
#

cheers ๐Ÿ™‚

vale hawk
crude rose
#

hex (should) be url safe, I think?

vale hawk
#

what does urlsafe mean here I didn't find any explanation in doc

crude rose
#

it probably means it won't generate characters that can't be used in a url

#

like , or /

#

but that's educated guess, not direct knowledge ๐Ÿ™‚

vale hawk
crude rose
#

so hex would generate characters that are either 0-9 or a-f

#

so, all of those would defacto be url safe

vale hawk
crude rose
#

yeah

vale hawk
#

I got it now

crude rose
#

hex should do that automatically

#

but it looks like urlsafe() adds in extra characters that hex doesn't

#

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_

crude rose
#

so yeah urlsafe would probably be a better choice for you

#

better chances at being unique