Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:06:06+00:00 2026-06-13T13:06:06+00:00

I have uuid field in pretty much every model. To avoid redundancy, I have

  • 0

I have uuid field in pretty much every model. To avoid redundancy, I have this BaseUuid abstract model to generate random uuid field:

class BaseUuid(models.Model):  # base abstract model with uuid field
    class Meta:
        abstract = True

    uuid = models.CharField(max_length=22, unique=True)

    def save(self, *args, **kwargs):
        if not self.uuid:
            uuid = shortuuid.uuid()[:10]
            while self.__class__.objects.filter(uuid=uuid).exists():
                uuid = shortuuid.uuid()[:10]
            self.uuid = uuid
        super(BaseUuid, self).save(*args, **kwargs)

As one may notice, the above code has a fixed uuid lengh (10).

Now I’d like to have the flexibility to set up that length. I want something like:

class BaseUuid(models.Model):  # base abstract model with uuid field
    class Meta:
        abstract = True

    uuid = models.CharField(max_length=22, unique=True)

    def __init__(self, uuid_length=10, *args, **kwargs):
        super(BaseUuid, self).__init__(*args, **kwargs)
        self.uuid_length = 22 if uuid_length > 22 else 5 if uuid_length < 5 else uuid_length

    def save(self, *args, **kwargs):
        if not self.uuid:
            uuid = shortuuid.uuid()[:self.uuid_length]
            while self.__class__.objects.filter(uuid=uuid).exists():
                uuid = shortuuid.uuid()[:self.uuid_length]
            self.uuid = uuid
        super(BaseUuid, self).save(*args, **kwargs)

The idea is to enable uuid_length parameter:

class SomeModel(BaseUuid(uuid_length=6), SomeOtherMixin):
    ...

But it doesn’t seem to work. Since the BaseUuid is subclassed from models.Model, “self.uuid_length” was treated as a model field while it is not defined.

I was wondering what other workarounds / technologies should I use? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:06:07+00:00Added an answer on June 13, 2026 at 1:06 pm

    Make uuid_length a class variable in the subclass, thus:

    class SomeModel(BaseUuid, SomeOtherMixin):
    
        uuid_length = 20 // or whatever...
    
        ...
    

    The reference to self.uuid_length in the save method of the base class will refer to the subclass’s uuid_length as you intend.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
i have the following (partial) hierarchy: @MappedSuperclass public abstract class PersistentEntity { @Id @GeneratedValue(generator=system-uuid)
I have this query in oracle that produces a count, by the UUID field,
I have this table layout: uuid bigint(20) unsigned primary auto_increment timestamp int(11) unsigned name
I have the following POJO: public class SampleBean1 { @Id @GeneratedValue(generator = system-uuid) @GenericGenerator(name
I have a class with members: std::string mName; boost::uuids::uuid mId; In the constructor I
In a model I usually put a uuid field for friendly URI, also a
I have a map in a multithreaded app mapping a class called uuid to
Say I have two UUID instances: uuid1 = UUID.randomUUID(); uuid2 = UUID.randomUUID(); If those
I am very interested in postgesql because they have a uuid data type. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.