--
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...@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.
--
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-fieldsHowever - 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 <valac...@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, POZ2930193What 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...@googlegroups.com.
The database usually handles this and you don't need to worry, there are many corner cases and DB systems are able to handle themBut they won't look like whatever format you would like, usually it is just a numberWhy do you need the IDs to look like that?
On Oct 22, 2017 6:53 PM, "Jack Zhang" <valac...@gmail.com> wrote:
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...@googlegroups.com.
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...@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/f7675114-b219-45a3-a7a6-372a223435f3%40googlegroups.com.
Hi,I think you are correct with your pseudocode - you can do a Model.objects.filter(unique_code==random_code).count() - and then loop on that. It should work.
--
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...@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/CA%2Be%2BciWUK0AUMJ5xO9AO26fjMcdNvrJ-0Bwpi0Rpj4S2fcS4CA%40mail.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, POZ2930193What 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.
I thought python's uuid.uuid4 guaranteed a unique value. Am I missing something?Mark
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/
--
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...@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/CA%2Be%2BciWFMQsFhgtWeRgLHfObas2LrwmpOJ0aw25ePPuajzKgOw%40mail.gmail.com.