pythondjangosatchmo

Attribute Error 'module' object has no attribute 'DateField'


I'm trying to extend an admin definition in Satchmo/Django and am getting the error "Attribute Error 'module' object has no attribute 'DateField'" trying to add a formfield_override to ProductOptions:

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)

Other types of fields that are in the model and should be available (CharField) generate the same error.

Any ideas as to where I should look to fix this?


Solution

  • 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,
        },
    }