Answer the question
In order to leave comments, you need to log in
How to create a slug in Django?
How to create a slug in Django?
I used SlugField and unique=True before . And in other examples I also saw this:
Now I learned about the primary_key setting and I had a question, why not create a slug differently:slug = models.SlugField(unique=True)
slug = models.SlugField(primay_key=True)
Answer the question
In order to leave comments, you need to log in
It is possible, but if you need to implement an m2m connection (for example, if posts belong to several groups), then you will have to duplicate this slug in the link table. I don’t know how convenient this is, because the slug can change at all - and you will have to change it in several tables. Or, say, you need to store "likes" of posts in a separate table. In one case, you will have a post_id there, on which a foreign key will be assigned to the id (pk) field in the table with posts, and in the other you will have to register the slug again. From memory, if anything, varchar takes up more space than int, indexes for such columns, respectively, too.
Moreover, if you store the history of changes in the same table, then you will have to duplicate the slug. So primay_key=True would suddenly no longer work in this field.
why notis really the wrong question. It will be true
Why do I need it?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question