AttributeError:类型对象“产品”没有属性“_meta”

Posted

技术标签:

【中文标题】AttributeError:类型对象“产品”没有属性“_meta”【英文标题】:AttributeError: type object 'Product' has no attribute '_meta' 【发布时间】:2016-03-05 11:31:51 【问题描述】:

我是 Python / Django 的新手,所以请多多包涵。我无法运行任何命令,并且终端每次都会产生相同的错误。

我的模型如下所示:

from django.db import models

# Create your models here.
class Product(models.Model):
    item_name = models.CharField(max_length=100)
    item_price = models.IntegerField()
    item_image = models.ImageField()

    class Meta:
        ordering = ['item_name']

Admin.py

from django.contrib import admin

# Register your models here.
from .models import Product

class Product(admin.ModelAdmin):
    pass

admin.register(Product)(admin.ModelAdmin)

追溯:-

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/yogesh/ShoppingSite/portal/admin.py", line 11, in <module>
    admin.register(Product)(admin.ModelAdmin)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/decorators.py", line 25, in _model_admin_wrapper
    admin_site.register(models, admin_class=admin_class)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py", line 85, in register
    if model._meta.abstract:
AttributeError: type object 'Product has no attribute '_meta'

【问题讨论】:

【参考方案1】:

从追溯看来,问题来自您的admin.py。如果您使用的是 admin.register 装饰器,请确保您使用正确:

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    pass

或者,您可以使用admin.site.register 函数:

class ProductAdmin(admin.ModelAdmin):
    pass

admin.site.register(Product, ProductAdmin)

【讨论】:

以上是关于AttributeError:类型对象“产品”没有属性“_meta”的主要内容,如果未能解决你的问题,请参考以下文章

django - AttributeError:类型对象'file'没有属性'set_user'

AttributeError:类型对象“用户”没有属性“用户名字段”

AttributeError:类型对象“numpy.ndarray”没有属性“__array_function__”

AttributeError:“UUID”对象在使用与后端无关的 GUID 类型时没有属性“replace”

AttributeError:类型对象 x 没有属性 y 以及与 Python 3.4 的其他一些不一致 [关闭]

AttributeError:类型对象“sklearn.tree._tree.TreeBuilder”没有属性“__reduce_cython__”