3

我正在尝试在 Satchmo/Django 中扩展管理员定义,但在尝试将 formfield_override 添加到 ProductOptions 时出现错误“属性错误‘模块’对象没有属性‘DateField’”:

from django.contrib import admin
from django.conf import settings
from widgets import AdminImageWidget, AdminDateWidget

from django.forms import models, ValidationError
from django.utils.translation import ugettext_lazy as _
from l10n.l10n_settings import get_l10n_setting
from l10n.utils import moneyfmt
from livesettings import config_value
from product.models import *
#from product.utils import import_validator, validate_attribute_value
#from satchmo_utils.thumbnail.field import ImageWithThumbnailField
#from satchmo_utils.thumbnail.widgets import AdminImageWithThumbnailWidget
#from django.http import HttpResponseRedirect
import re

from product.admin import *


class ProductImage_Inline(ProductImage_Inline):
    model = ProductImage
    extra = 0

    formfield_overrides = {
        ImageWithThumbnailField : {'widget' : AdminImageWidget},
    }

class ProductOptions(ProductOptions):

    #list_display += ('slug', 'name', 'formatted_price', 'formatted_inventory', 'active','featured', 'get_subtypes')
    list_display = ('name', 'sku', get_categories, 'formatted_price', 'active','featured', 'get_subtypes')

    list_display_links = ('name',)
    list_filter = ('category', 'date_added','active','featured')
    actions = ('make_active', 'make_inactive', 'make_featured', 'make_unfeatured')
    fieldsets = (
        (None, {'fields': ('name', 'slug', 'sku')}), 
        (None, {'fields': ('site', 'category')}), 
        (None, {'fields': ('description', 'short_description', 'date_added','active', 'featured', 'items_in_stock','total_sold','ordering', 'shipclass')}), 
        (_('Meta Data'), {'fields': ('meta',), 'classes': ('collapse',)}),
        (_('Item Dimensions'), {'fields': (('length', 'length_units','width','width_units','height','height_units'),('weight','weight_units')), 'classes': ('collapse',)}),
        (_('Tax'), {'fields':('taxable', 'taxClass'), 'classes': ('collapse',)}),
        (_('Related Products'), {'fields':('related_items','also_purchased'),'classes':('collapse',)}))

    search_fields = ['slug', 'sku', 'name', 'category__name']
    inlines = [ProductAttribute_Inline, Price_Inline, ProductImage_Inline]
    if get_l10n_setting('show_admin_translations'):
        inlines.append(ProductTranslation_Inline)
    filter_horizontal = ('category',)

#     formfield_overrides = {
#       models.ImageField: {'widget': AdminImageWidget}}

    formfield_overrides = {
        models.DateField: {
            'widget': AdminDateWidget,
            'input_formats': settings.VALID_DATE_FORMATS,
            },
        }

    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(ProductOptions, self).formfield_for_dbfield(db_field, **kwargs)
        fieldname = db_field.name
        if fieldname in ("length_units", "width_units", "height_units"):
            field.initial = default_dimension_unit()
        elif fieldname == "weight_units":
            field.initial = default_weight_unit()
        return field

admin.site.unregister(Product)
admin.site.register(Product, ProductOptions)

模型中应该可用的其他类型的字段 (CharField) 会生成相同的错误。

关于我应该在哪里解决这个问题的任何想法?

4

1 回答 1

2

DateField is defined in django.db.models, notdjango.forms.models. You need to import django.db.models as well.

Perhaps use:

from django.db import models as db_models

formfield_overrides = {
    db_models.DateField: {
        'widget': AdminDateWidget,
        'input_formats': settings.VALID_DATE_FORMATS,
    },
}
于 2013-05-05T17:34:42.613 回答