Immediately Need Help

47 views
Skip to first unread message

Kritika Paul

unread,
Feb 19, 2021, 5:01:29 AM2/19/21
to Django users
 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

Chetan Ganji

unread,
Feb 19, 2021, 5:22:54 AM2/19/21
to django...@googlegroups.com
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 😋


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/690debd2-46e0-497f-92a8-3abbc391b97dn%40googlegroups.com.

Kritika Paul

unread,
Feb 19, 2021, 5:25:59 AM2/19/21
to django...@googlegroups.com
I need to fetch it from different models. If you want I can share models snapshot



--

shubham vashisht

unread,
Feb 19, 2021, 5:29:26 AM2/19/21
to django...@googlegroups.com
Chetan
If any row is deleted from the table, then your logic will fail to return the unique code

Kritika Paul

unread,
Feb 19, 2021, 5:35:51 AM2/19/21
to django...@googlegroups.com
No row will be deleted, also, the code created once will never be assigned to any other space

Chetan Ganji

unread,
Feb 19, 2021, 5:38:00 AM2/19/21
to django...@googlegroups.com
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.

Kritika Paul

unread,
Feb 19, 2021, 5:50:27 AM2/19/21
to django...@googlegroups.com
class SpaceCategory(BaseModel):
    __tablename__ = "space_categories"
    
    name = db.Column(db.String(255))
    description = db.Column(db.Text)
    image_url = db.Column(db.Text)
    code = db.Column(db.String(100))
    space_sub_category = db.relationship('SpaceSubCategory'backref=db.backref("space_category"uselist=False))
    
class Cities(BaseModel):
    __tablename__ = "cities"
    name = db.Column(db.String(255))
    code = db.Column(db.String(100))
    is_deleted = db.Column(db.Boolean,default=False)
These are the models,
There is one other model where I have to save Space_code

Now, Space Category is CW,COM,MS
City Code is NOIDA, DELHI,KOlkata

Space code to be generated such that Numbers should increase Space Category Wise


Chetan Ganji

unread,
Feb 19, 2021, 6:02:29 AM2/19/21
to django...@googlegroups.com
Show the model where you save Space_code

Kritika Paul

unread,
Feb 19, 2021, 7:09:32 AM2/19/21
to django...@googlegroups.com
class Spaces(BaseModel):
    __tablename__ = "spaces"
    name = db.Column(db.String(255))
    user_id = db.Column(db.Integer)
    status = db.Column(
        db.String(100), default=SpaceStatusType.PENDING.value)
    space_category_id = db.Column(
        db.Integer, db.ForeignKey("space_categories.id"))
    space_company_id = db.Column(
        db.Integer, db.ForeignKey("space_companies.id"))
    space_detail_id = db.Column(db.Integer, db.ForeignKey("space_details.id"))
    space_broker_id = db.Column(db.Integer, db.ForeignKey("space_brokers.id"))
    space_building_particulars_id = db.Column(db.Integer, db.ForeignKey("space_building_particulars.id"))
    space_commercial_products_id = db.Column(db.Integer, db.ForeignKey("space_commercial_products.id"))
    space_document_id = db.Column(db.Integer, db.ForeignKey("space_documents.id"))
    is_deleted = db.Column(db.Boolean,default=False)
    is_completed = db.Column(db.Boolean,default=False)
    space_category = db.relationship("SpaceCategory"backref=db.backref(
        "spaces"uselist=Truelazy="dynamic"))
    space_company = db.relationship("SpaceCompany"backref=db.backref(
        "spaces"uselist=Truelazy="dynamic"))
    space_detail = db.relationship("SpaceDetail"backref=db.backref(
        "spaces"uselist=Truelazy="dynamic"))
    space_broker = db.relationship("SpaceBroker"backref=db.backref(
        "spaces"uselist=Truelazy="dynamic"))
    space_building_particulars = db.relationship("SpaceBuildingParticular"backref=db.backref(
        "spaces"uselist=Truelazy="dynamic"))
    space_commercial_products=db.relationship("SpaceCommercialProduct"backref=db.backref(
        "spaces"uselist=Truelazy="dynamic"))
    space_document=db.relationship("SpaceDocument"backref=db.backref(
        "spaces"uselist=Truelazy="dynamic"))



Need to save in name field


shubham vashisht

unread,
Feb 19, 2021, 7:47:23 AM2/19/21
to django...@googlegroups.com
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

Chetan Ganji

unread,
Feb 20, 2021, 11:40:51 AM2/20/21
to django...@googlegroups.com
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


Reply all
Reply to author
Forward
0 new messages