Shubham is right. However, as no row will be deleted.
Also, the code created once will never be assigned to any other space.
It wont matter!

Formula : LM-categorycode-citycode-current_index
e.g. "LM-CW-DELHI-000000001"
# Remove dashses from the string, they are added only to make it easier to
understand

String after the citycode is called as current_index and it has to be of a
standard length
e.g. 9. We will call this length as Z_FILL_INDEX. You can modify this
Z_FILL_INDEX as required.
Hence, Z_FILL_INDEX = 9. In above example, current_index = "000000001"

As no row will be deleted, also, the code created once will never be
assigned to any other space
Hence, current_index will be 1 or plus 1 of the latest one
Below code is for django ORM, IDK sqlalchemy version of it :P
I dont see how Space is related to a City, you can figure that part
yourself.
if current_index > 999999999, code will break. 10 Cr is a big no.
However, if current_index could be bigger than that, pick a bigger no for
Z_FILL_INDEX.

In case you run out of index in the future, you can write a script to add
few extra zeros
to current index part of the code or add pick a bigger no for Z_FILL_INDEX
initially e.g. 12.

MUST DO : ADD UNIQUE CONSTRAINT ON THE name FIELD OF THE Spaces Model.
Z_FILL_INDEX = 9

last_code = Spaces.objects.filter(space_category="blah").order_by(
"-created_at").first()
if last_code is None:
current_index = "1".zfill(Z_FILL_INDEX)
else:
current_index = str(int(last_code[-Z_FILL_INDEX:]) + 1)

unique_code ="LM" + categorycode + citycode + current_index




Regards,
Chetan Ganji
+91-900-483-4183
ganji.che...@gmail.com
http://ryucoder.in


On Fri, Feb 19, 2021 at 6:16 PM shubham vashisht <shubhva...@gmail.com>
wrote:

> If there are 4 rows which have categorycode="blah" and
> citycode="blah-blah", then the space code will be something like this,
> LMblahblah-blah1, LMblahblah-blah2, LMblahblah-blah3, LMblahblah-blah4.
> But if you delete row with spacecode different from LMblahblah-blah4, then
> your code will generate space code LMblahblah-blah4
>
> On Fri, 19 Feb 2021, 16:07 Chetan Ganji, <ganji.che...@gmail.com> wrote:
>
>> Yes kritika show all the models.
>>
>> How shubham?
>> If an entry is deleted, unique code will also be deleted. Next time it
>> gets generated, it might be assigned to different entry, but it still be
>> unique.
>>
>> As kritika clarified already, so it wont be a problem.
>>
>> On Fri, Feb 19, 2021, 3:58 PM shubham vashisht <shubhva...@gmail.com>
>> wrote:
>>
>>> Chetan
>>> If any row is deleted from the table, then your logic will fail to
>>> return the unique code
>>>
>>> On Fri, 19 Feb 2021, 15:52 Chetan Ganji, <ganji.che...@gmail.com> wrote:
>>>
>>>> Hi Kritika
>>>>
>>>> Ye chanel mein firangi log bhi hai, unko hinglish kaisa samjhenga? 😂
>>>>
>>>> Teko field pe unique constraint lagana padenga
>>>>
>>>> Teko no wapas 1 se start karneke liye query maarana padenga
>>>>
>>>> last_no = Model.objects.filter(categorycode="blah",
>>>> citycode="blah-blah").count()
>>>>
>>>> unique_code ="LM" + categorycode
>>>> + citycode + str(last_no + 1)
>>>>
>>>>
>>>> Aisa code likkha toh result milenga 😋
>>>>
>>>>
>>>> On Fri, Feb 19, 2021, 3:31 PM Kritika Paul <rkritikapau...@gmail.com>
>>>> wrote:
>>>>
>>>>>  I have to create a unique code for each space saved in dbAb db me
>>>>> save krne k lie, wo multiple part me save ho rha hai, multiple models bhi
>>>>> hai uske.1st model se mujhe space category ka name uthana hai
>>>>> 2nd se city code
>>>>> Category alag alg ho skti hai
>>>>> To code genrate hoga LM-categorycode-citycode-00001Next time jb
>>>>> category, B hogi to number 1 se hi start hoga, ni to category same hai to
>>>>> fr se 2 allot ho jaega
>>>>> Eg Category - CW, COM
>>>>> City - Noida, DelhiSpace 1 - Cw category
>>>>> Code space 1- LMCWNoida00001Space2 - CW Category
>>>>> CODE SPACE2 - LMCWDelhi00002Space 3 - COM category
>>>>> CODE SPACE 3- LM-COM-NOIDA0001
>>>>>
>>>>> --
>>>>> 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+unsubscr...@googlegroups.com.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/django-users/690debd2-46e0-497f-92a8-3abbc391b97dn%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/django-users/690debd2-46e0-497f-92a8-3abbc391b97dn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>>>> 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+unsubscr...@googlegroups.com.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/django-users/CAMKMUjs6q%2B6Xer6-MRu%3DYjhKq5EYNXahEji%2BY5HBerTaNQ1Law%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/django-users/CAMKMUjs6q%2B6Xer6-MRu%3DYjhKq5EYNXahEji%2BY5HBerTaNQ1Law%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
>>> 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+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAJW_r64v1-f85ViX%2BnCFdBrupiXi5Ba%3Dzec6xoSvuZnR1jaMEg%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-users/CAJW_r64v1-f85ViX%2BnCFdBrupiXi5Ba%3Dzec6xoSvuZnR1jaMEg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAMKMUjsJR2B7LDHujo_bXxyh1s7WZMqMwgENMQmwiX%3Dpe5QHTg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAMKMUjsJR2B7LDHujo_bXxyh1s7WZMqMwgENMQmwiX%3Dpe5QHTg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAJW_r671m7KANhpAdSPuG7AabVacUOsVcgdhPgH2j8HruNkOgQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAJW_r671m7KANhpAdSPuG7AabVacUOsVcgdhPgH2j8HruNkOgQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMKMUjvjf3iF9-EuBGN%2BQkzuQds4oUMh_Os-KmXd3LYn64pcsQ%40mail.gmail.com.

Reply via email to