嘿,我正在阅读《实用Django项目》一书,并尝试预填充SlugField,但这没有发生。

#models.py
from django.db import models

class Category (models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField (unique=True)
    description = models.TextField()

    class Meta:
        verbose_name_plural = "Categories"

    def __unicode__(self):
        return self.title
#admin.py
from django.contrib import admin
from coltrane.models import Category

class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}

admin.site.register(Category)

最佳答案

admin.site.register(Category)更改为admin.site.register(Category, CategoryAdmin)

关于Django 'prepopulated_fields'无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14482418/

10-16 18:31