How to have models.UUIDField field without dash

Use CharField field instead of UUIDField,

def generate_uuid():
    return uuid.uuid4().hex


class Profile(models.Model):
    api_key = models.CharField(default=generate_uuid, editable=False, unique=True, max_length=40)

Are you using PostgreSQL by any chance? The UUIDField may be using the native uuid type for the column. It stores it efficiently using only 16 bytes (without dashes). If that is the case, it is not storing the dashes, only showing them when you select.

The good news is that in Python code you are getting a UUID object, so you can do self.api_key.hex to get a string without dashes.

Tags:

Python

Django