0

I am trying to prepopulate the SlugField but it's not happening. I use python 3.6.1 and Django 1.11.

Here is my code.

models.py

class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Черновик'),
        ('published', 'Опубликовано'),
    )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    author = models.ForeignKey(User,
                           related_name='blog_posts',
                           default=1,)
    body = RichTextUploadingField(blank=True,
                              default='',
                              config_name='awesome_ckeditor')
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,
                          choices=STATUS_CHOICES,
                          default='draft')
    objects = models.Manager()
    published = PublishedManager()

    def __str__(self):
        return self.title

    class Meta:
       ordering = ('-publish',)


    def get_absolute_url(self):
        return reverse('blog:post_detail',
                       args=[self.publish.year,
                             self.publish.strftime('%m'),
                             self.publish.strftime('%d'),
                             self.slug])

And this is

admin.py

class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug', 'author', 'publish',
                'status')
    list_filter = ('status', 'created', 'publish', 'author')
    search_fields = ('title', 'body')
    prepopulated_fields = {"slug": ("title",)}
    raw_id_fields = ('author',)
    date_hierarchy = 'publish'
    ordering = ['-publish', 'status']


admin.site.register(Post, PostAdmin)

Site is hosted on heroku. Maybe I can auto generate slug field in another way?

Community
  • 1
  • 1
  • Do you have any javascript errors on the page? – Dan Moica Nov 01 '17 at 09:52
  • Yes. I have this. "Uncaught ReferenceError: grp is not defined at actions.min.js:154 Uncaught ReferenceError: grp is not defined at prepopulate.min.js:42 Uncaught TypeError: $ is not a function at change_form.js:5 at change_form.js:20 Uncaught TypeError: $ is not a function at prepopulate_init.js:3 at prepopulate_init.js:10" – Даниил Nov 01 '17 at 10:00
  • The javascript error prevents Django js to pre-polulate the slug field. You can try to figure out the javascript error about the grapelli addon (see this question https://stackoverflow.com/questions/12025231/django-cms-with-grappelli-messed-up-the-layout-for-admin-cms-pages ) or override the save method to populate yourself the slug field. – Dan Moica Nov 01 '17 at 10:09
  • Thank you. I will read and try to fix JS error. – Даниил Nov 01 '17 at 11:49

1 Answers1

1
from autoslug import AutoSlugField

slug = AutoSlugField(populate_from='title', unique_for_date='publish')
Pushplata
  • 552
  • 4
  • 10