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 syncdb

Traceback (most recent call last):
  File "", line 11, in <module>
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\", line 438, in execute_manager
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\", line 379, in execute
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\", 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\", line 217, in execute
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\management\", 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\", 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\", line 146, in get_app_errors
  File "C:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\db\models\", 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\", 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\", line 35, in import_module
  File "..\tso\blog\", line 50, in <module>
    class Category(models.Model):
  File "..\tso\blog\", 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\", 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\", 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:

# 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')}, MyModelAdmin)

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


Comments powered by Disqus