Sunday, October 22, 2017

Re: Generating a unique 10 digit ID for each model instance

Hi,

When you say "globally unique" - I am supposing you mean within your application?

What you need to do is set a field to be the primary key, see : https://docs.djangoproject.com/en/1.11/topics/db/models/#automatic-primary-key-fields

However - it would be simpler to use the standard primary key auto id field, and then add another field to hold your unique ID in it. This could then be created on the pre_save signal and you could write something that randomly generates the unique ID field. See https://docs.djangoproject.com/en/1.11/topics/signals/

Best regards,

Andréas

2017-10-22 17:52 GMT+02:00 Jack Zhang <valachio123@gmail.com>:
Let's say I have a model called 'Dogs'.  Users can create instances of Dogs.  For every Dogs instance that is created, I want to assign a globally unique ID to it.  The ID will be 3 capitalized letters followed by 7 numbers.  E.g. ABC1234567, POZ2930193

What is the easiest way to go about doing this?  I looked into UUID but it generates a longer string.  Keep in mind that the ID must be globally unique - so after one has been created, it should not be created again.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0cd8e33c-8c92-471c-9c36-ac82749750b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCePgdn1VmrDq%3DHKdf2MctZF6TLFNVo_HLmrO4a%3DbJ852g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment