Django - Problems saving objects with ID of type UUID in admin

0

I work on a Python with Django Framework project and recently decided to change the generation of the integer to UUID IDs for merge (we have data that comes from different bases and needs to be aggregated on a central basis). All of our entities have been changed according to 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 possible to insert data directly through the database and the admin is not possible although the message of success continues to be presented.

    
asked by anonymous 11.10.2017 / 16:16

1 answer

1

After much struggle I discovered the problem. I was putting the default UUIDField this way

default=uuid.uuid4()

When should it be?

default=uuid.uuid4

If you repair the difference in parentheses. Placing parentheses means calling a method and when it is unrelated, it refers to the method. In short, whenever the server starts the first time an object is created the method is called and its value is stored in the cache and in the next calls it uses the same generated value of uuid. Changing for reference ensures that whenever an object is the method it is always called.

The tip is for everyone

    
16.10.2017 / 10:36