Django - Problems saving objects with UUID type ID in admin

Asked

Viewed 100 times

0

Work on a project in Python with Framework Django and recently we decided to change the generation of Ids integer to UUID on grounds of merge data before synchronization (we have data that comes from different databases and needs to be aggregated on a central basis). All our entities have been changed as per the example below (according to the documentation consulted)

class TestModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4(), editable=False, auto_created=True)
    test_name = models.TextField("Test Name", blank=True)
    test_surname = models.TextField("Test Surname", blank=True)

After this change it is only possible to enter data directly through the database and by admin is no longer possible although the message of success continues to be presented.

1 answer

1


After much struggle I discovered the problem. I was setting the default for Uuidfield this way

default=uuid.uuid4()

when it must be so

default=uuid.uuid4

If you notice the difference in parentheses. Placing parentheses means called a method and when this without parentheses is referred to the method. In short, whenever the server starts the first time an object is created the method and called and its value stored in the cache and next calls it uses the same value generated from uuid. Switching to reference ensures that whenever an object is the method is always called.

Here’s a tip for everyone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.