Search code examples
pythondjangoslugdjango-1.11

Django 1.11.8 - Slug - Unexpected Keyword argument


I am new to django and I have started building my first app a couple of days ago. I first went through the MDN Tutorial and the Django Tutorial - building 2 "dummy" apps.

I am currently trying to develop a photo gallery app that will display all pictures belonging to a certain album. I am able to accomplish that using in my urls.py and a class-based view, though, I would like to get a cleaner URL using . I would like my URL to look like mysite.com/myapp/my-album-name vs mysite.com/myapp/1

I know they are similar questions that have been asked here, I've went through most of them, read the Django doc about Single Object Mixins, Class Based Views and I am still not understanding how to get slug to work.

So far I've tried to:

  1. Implement a slug field in my models and the corresponding slug values in my admin.py file:

models

class Album(models.Model):
    album_name = models.CharField(max_length=100, help_text="Enter the name of your album" )
    album_creation_date = models.DateField(auto_now_add=True)
    album_modified_date = models.DateField(auto_now=True)
    slug = models.SlugField(max_length=50)

Views

class albumPictures(generic.DetailView):
    model = Album 

Admin

class AlbumAdmin(admin.ModelAdmin):
    list_display = ('album_name', 'album_creation_date')
    slug = prepopulated_fields = {'slug': (Album.album_name)}

admin.site.register(Album, AlbumAdmin)

Error Message: <class 'Albums.admin.AlbumAdmin'>: (admin.E030) The value of 'prepopulated_fields["slug"][0]' refers to '<django.db.models.query_utils.DeferredAttribute object at 0x107f234a8>', which is not an attribute of 'Albums.Album'.

  1. Implement a slug field in my models with a prepopulated_from=album_name or prepopulated_fields=album_name

models (1)

class Album(models.Model):
    album_name = models.CharField(max_length=100, help_text="Enter the name of your album" )
    album_creation_date = models.DateField(auto_now_add=True)
    album_modified_date = models.DateField(auto_now=True)
    slug = models.SlugField(prepopulated_from=album_name)

models (2)

class Album(models.Model):
    album_name = models.CharField(max_length=100, help_text="Enter the name of your album" )
    album_creation_date = models.DateField(auto_now_add=True)
    album_modified_date = models.DateField(auto_now=True)
    slug = models.SlugField(prepopulated_fields=album_name)

Views

class albumPictures(generic.DetailView):
    model = Album 

Error Message: TypeError: __init__() got an unexpected keyword argument 'prepopulated_from'

Thank you for your help


Solution

  • The models.SlugField() does not support prepopulated_from or prepopulated_fields, so in your model you should use

    slug = models.SlugField(max_length=50)
    

    instead of

    slug = models.SlugField(prepopulated_from=album_name)
    

    or

    slug = models.SlugField(prepopulated_fields=album_name)
    

    Secondly, in your ModelAdmin class, you should use a tuple of strings with the prepopulated_fields option:

    class AlbumAdmin(admin.ModelAdmin):
        list_display = ('album_name', 'album_creation_date')
        prepopulated_fields = {'slug': ("album_name",)}