Since Django 1.2.1 'prepopulated_fields' w

2019-07-17 05:07发布

问题:

Since Django 1.2.1 'prepopulated_fields' won't prepopulate in the admin.

prepopulated_fields = {'slug': ('title',)} doesn't seem to work since uploading to a Django 1.2.1 server after developing on a 1.1.1.

What changed?

I read http://code.djangoproject.com/wiki/NewformsAdminBranch#Changedprepopulate_fromtobedefinedintheAdminclassnotdatabasefieldclasses but didn't find a way to fix it, my code seems good.

Ideas? Code:

class Data(models.Model):
    title = models.CharField(max_length=50)
    slug = models.SlugField(max_length=50, unique=True, help_text='Unique value for product page URL, created from name.')


class DataAdmin(admin.ModelAdmin):
    list_display = ('title', 'user', 'category')
    list_filter = ('user', 'category')
    ordering = ('title',)
    search_fields = ('title',)
    prepopulated_fields = {'slug': ('title',)}  
admin.site.register(Data, DataAdmin)

回答1:

It happened to me exactly when upgrading from django 1.1.1 to 1.2.1. It is because the media/admin directory it has changed, before it was something like that: media/admin/js/admin and now is: admin/media/js/admin. What I did was to change in settings ADMIN_MEDIA_PREFIX = '/media/admin/'

To be sure when you are in your admin page, the one that does not prepopulate, run firebug and check from where that page is trying to fetch the js files. You will see that there is a discrepancy between that location and the actual location of those js files in Django 1.2.1.



回答2:

Did you read the current documentation for prepulated_fields ?

It would help if you showed your code, but you just place it under your Admin class, it's a pretty straight forward setup.



回答3:

I can say with certainty that prepopulated_fields still works as indicated in the docs. Your code looks sound, but here are some possible problems I can think of:

  1. Javascript is disabled and/or your admin media links are broken.
  2. You've got a typo somewhere in the names of your fields.
  3. You have something cached in your browser that is preventing the javascript from working correctly.