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.
The reason why I thought you should use the signals is because thats how I have done the same things in the past. However, you can just overload the save method and then do it there (if there is no PK). Regards, Andréas 2017-10-22 20:53 GMT+02:00 Jack Zhang <[email protected]>: > Hi Andréas, > > Yes, by globally unique I mean a unique 10-digit ID is assigned to every > model instance of 'Dogs' within my application. This ID will be shown > publicly on the website to allow easy search access to a specific model > instance. > > I think you are right about creating a new field to hold the unique ID, > instead of using the primary key. Generating the ID is not too difficult, > but how can I ensure that I won't generate a duplicate ID? From briefly > thinking about it, this is what I got... > > When a new instance is created, generate *new ID* > For every *existing ID* in the database: > If *new ID* == *existing ID* in the database: > Generate *new ID* and re-run the for loop > If *new ID* does not match any *existing ID * in the database, proceed > with continuing the model instance. > > Also I read up on Signals, but is it necessary? > > > On Sunday, October 22, 2017 at 12:30:02 PM UTC-4, Andréas Kühne wrote: >> >> 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/#a >> utomatic-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 <[email protected]>: >> >>> 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 [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at https://groups.google.com/group/django-users. >>> To view this discussion on the web visit https://groups.google.com/d/ms >>> gid/django-users/0cd8e33c-8c92-471c-9c36-ac82749750b4%40googlegroups.com >>> <https://groups.google.com/d/msgid/django-users/0cd8e33c-8c92-471c-9c36-ac82749750b4%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> 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 [email protected]. > To post to this group, send email to [email protected]. > 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 > <https://groups.google.com/d/msgid/django-users/f7675114-b219-45a3-a7a6-372a223435f3%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > 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 [email protected]. To post to this group, send email to [email protected]. 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/CAK4qSCfWBV6k0xvW4k79J6Rj6B8rS9a1%3DL-H4qrU2hLUfw%3D1iw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

