Based on the tutorials I did and the books I read I decided to define my models with id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) instead of id = models.BigAutoField(primary_key=True)
I noticed that the auth models use id = models.BigAutoField(primary_key=True) and I figure this is related to this setting: DEFAULT_AUTO_FIELD = ‘django.db.models.BigAutoField’
I wonder whether there are any disadvantages to changing this setting to DEFAULT_AUTO_FIELD = ‘django.db.models.UUIDField’ so I do not repeat myself by having this line of code: id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) in all my models.
Kind regards,
Johanna
Yes, there are likely many possible problems that could occur by doing that. Unless you know that every Django model and third-party package would work with that setting, you’re possibly causing numerous problems for yourself in areas that you’re not likely going to want to have to clean up.
I have no idea how many models work under the assumption that the primary key field is an integer with the automatically-defined id field - or how many other models referring to those original models expect the ForeignKeyField to also be an integer.
You could try it and see - delete and recreate (if necessary) your database, delete all your migration files, change this setting, do a makemigrations and migrate - and see if everything works.
Another option would be to create an abstract parent model class with that field defined and have your models inherit from it instead of models.Model. Or you could create it as a mixin that is added to the standard class. (We do something similar to this for a set of “tracking and status” fields in our models.)
Anyway, I probably wouldn’t recommend trying it unless you’re prepared to address a potentially large number of unanticipated side effects. (As one example, think of all the urls that may be defined with <int:pk>
that immediately become invalid.)
1 Like
Hi Ken,
Thanks for your extensive explanation of the issue. As far as I can see user_id, group_id and permission_id aren’t used in the URLs, so I leave my models as they are.
Kind regards,
Johanna