您好我一直试图覆盖Django Mezzanine的产品状态字段(草稿/已发布)并在管理员中添加新选项(草稿/已发布/研究)但没有运气。我是Django的新手试图在Django而不是Wordpress上开始一个新项目,所以请看看我对django结构的了解不足。
这是我的代码:
购物/购物车/应用/夹层/ models.py
from django.db import models
from django.utils.translation import ugettext, ugettext_lazy as _
from mezzanine.core.models import Slugged, MetaData, TimeStamped
CONTENT_STATUS_DRAFT = 1
CONTENT_STATUS_PUBLISHED = 2
CONTENT_STATUS_RESEARCH = 3
CONTENT_STATUS_CHOICES = (
(CONTENT_STATUS_DRAFT, _("Draft")),
(CONTENT_STATUS_PUBLISHED, _("Published")),
(CONTENT_STATUS_RESEARCH, _("Research")),
)
class Displayable(Slugged, MetaData, TimeStamped):
status = models.IntegerField(_("Status"),
choices=CONTENT_STATUS_CHOICES, default=CONTENT_STATUS_RESEARCH,
help_text=_("With Draft chosen, will only be shown for admin users "
"on the site."))
publish_date = models.DateTimeField(_("Published from"),
help_text=_("With Published chosen, won't be shown until this time"),
blank=True, null=True, db_index=True)
Displayable.default=CONTENT_STATUS_RESEARCH
Displayable.choices=CONTENT_STATUS_CHOICES
购物/购物车/应用/夹层/ admin.py:
from django.contrib import admin
from mezzanine.conf import settings
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _
from cart.apps.mezzanine.models import Displayable
from mezzanine.core.admin import BaseTranslationModelAdmin
class DisplayableAdminForm(ModelForm):
def clean_content(form):
status = form.cleaned_data.get("status")
content = form.cleaned_data.get("content")
if status == CONTENT_STATUS_PUBLISHED and not content:
raise ValidationError(_("This field is required if status "
"is set to published."))
return content
class DisplayableAdmin(BaseTranslationModelAdmin):
list_display = ("title", "status", "admin_link")
list_display_links = ("title",)
list_editable = ("status",)
list_filter = ("status", "keywords__keyword")
date_hierarchy = None if settings.USE_MODELTRANSLATION else "publish_date"
radio_fields = {"status": admin.HORIZONTAL}
fieldsets = (
(None, {
"fields": ["title", "status", ("publish_date", "expiry_date")],
}),
(_("Meta data"), {
"fields": ["_meta_title", "slug",
("description", "gen_description"),
"keywords", "in_sitemap"],
"classes": ("collapse-closed",)
}),
)
form = DisplayableAdminForm
admin.site.register(Displayable, DisplayableAdmin)
settings.py:
INSTALLED_APPS = (
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.redirects",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.sitemaps",
"django.contrib.staticfiles",
"mezzanine.boot",
"mezzanine.conf",
"mezzanine.generic",
"mezzanine.pages",
"cartridge.shop",
"mezzanine.blog",
"mezzanine.forms",
"mezzanine.galleries",
"mezzanine.twitter",
"cart.apps.mezzanine",
"mezzanine.core",
)
版本:
夹层4.2.3 Django 1.10.8 Python 2.7.6 SQLite 3.8.2
没有错误,dev服务器加载正常。我错过了什么步骤?我是否需要为Mezzanine配置app.py?感谢您的帮助。