I create a model article on django 1.10. I enter the title of the article in Russian in the admin panel. prepopulated_fields = {'slug' :( 'name',)} works fine, that is, 'Test' is translated into 'test'.

But when writing to the database, only numbers and Latin characters enter the field. Why it happens? Although if you originally write the name in English, then everything is saved to the database normally. SQLite database.

models.py

class Rubric(models.Model): name = models.CharField(max_length=100, unique=True) slug = models.SlugField(unique=True) def save(self, *args, **kwargs): self.slug = slugify(self.name) super(Rubric,self).save(*args, **kwargs) class Meta: verbose_name_plural = 'Рубрики' def __str__(self): return self.name 

admin.py

  from django.contrib import admin from blog.models import Rubric, Article class RubricAdmin(admin.ModelAdmin): prepopulated_fields = {'slug':('name',)} admin.site.register(Rubric, RubricAdmin) class ArticleAdmin(admin.ModelAdmin): prepopulated_fields = {'slug':('name',)} admin.site.register(Article, ArticleAdmin) 

    1 answer 1

    Your code does not show where you are importing slugify. I assume that this is a string in models.py: from django.utils.text import slugify

    If so, then see the documentation for django.utils.text.slugify It says that the default encoding is ascii. If you want to use utf-8 conversion, do:

     def save(self, *args, **kwargs): self.slug = slugify(self.name, allow_unicode=True) super(Rubric, self).save(*args, **kwargs) 

    And if you use form filling only in the admin panel, then generally remove this save method from the model.