我是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'
答案 0 :(得分:2)
从追溯来看,问题来自你的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)