Errors with the prepopulate_from SlugField parameter in Django 1.2.1

I recently upgraded to Django version 1.2.1 and I immediatelly noticed that some of my models were broken with the following error:

C:\test>python manage.py syncdb

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\__init__.py", line 438, in execute_manager
    utility.execute()
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\base.py", line 217, in execute
    self.validate()
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\base.py", line 245, in validate
    num_errors = get_validation_errors(s, app)
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\validation.py", line 28, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\db\models\loading.py", line 146, in get_app_errors
    self._populate()
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\db\models\loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\db\models\loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "..\tso\blog\models.py", line 50, in <module>
    class Category(models.Model):
  File "..\tso\blog\models.py", line 53, in Category
    slug = models.SlugField(unique=True, prepopulate_from=('title',), help_text='Used in the category URL. Must be unique')
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\db\models\fields\__init__.py", line 988, in __init__
    super(SlugField, self).__init__(*args, **kwargs)
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\db\models\fields\__init__.py", line 542, in __init__
    super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'prepopulate_from'

So I did a little bit of research on this problem and I found the following explanation for what is going on:

Changed prepopulate_from to be defined in the Admin class, not database field classes

As of [4446], the prepopulate_from option to database fields no longer exists. It's been discontinued in favor of the new prepopulated_fields option on class Admin. The new prepopulated_fields option, if given, should be a dictionary mapping field names to lists/tuples of field names. This change was made in an effort to remove admin-specific options from the model itself. Here's an example comparing old syntax and new syntax:

# OLD:

class MyModel(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    slug = models.CharField(max_length=60, prepopulate_from=('first_name', 'last_name'))

    class Admin:
        pass

# NEW:

class MyModel(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    slug = models.CharField(max_length=60)

from django.contrib import admin

class MyModelAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('first_name', 'last_name')}

admin.site.register(MyModel, MyModelAdmin)

The above is taken from the Django wiki page that talks about the newforms-admin branch

Comments

Comments powered by Disqus