Slug field error in django
I am trying to integrate the following Blog app to my site,I get the following error for the slug field ,how to resolve this issue
TypeError at /login/
__init__() got an unexpected ke开发者_开发问答yword argument 'prepopulate_from'
Request Method: GET
Request URL: http://192.168.254.35/accounts/login/new_wind/
Django Version: 1.2.1 SVN-1957
Exception Type: TypeError
Exception Value:
__init__() got an unexpected keyword argument 'prepopulate_from'
This is my models.py
from django.db import models
from datetime import datetime
class BlogPost(models.Model):
title = models.CharField(max_length=128)
slug = models.SlugField(prepopulate_from=('title',))
body = models.TextField()
published = models.BooleanField(default=True)
date_posted = models.DateTimeField(default=datetime.now)
def __unicode__(self):
return self.title
class Admin:
pass
The error says it all: a SlugField
does not take the keyword argument prepopulate_from
. The docs back this up - it only takes the argument max_length
in addition to the standard argument. This should be done in the admin class instead.
Edit: So your model file would be:
from django.db import models
from datetime import datetime
class BlogPost(models.Model):
title = models.CharField(max_length=128)
slug = models.SlugField()
body = models.TextField()
published = models.BooleanField(default=True)
date_posted = models.DateTimeField(default=datetime.now)
def __unicode__(self):
return self.title
and the admin.py file for the same application would be:
from django.contrib import admin
from myapp.models import BlogPost
class BlogPostAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
admin.site.register(BlogPost, BlogPostAdmin)
If you don't understand how the admin site works, see Part 2 of the official Django tutorial.
It seems the way to prepopulate has been updated in django, see this question: Is there an easy way to populate SlugField from CharField?
精彩评论