我需要一个帮助。我在使用Python和Django迁移模型文件时遇到以下错误。我在解释下面的错误。
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 327, in execute
self.check()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
issues.extend(super(Command, self)._run_checks(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/opt/lampp/htdocs/carClinic/carClinic/urls.py", line 7, in <module>
url(r'^admin/', admin.site.urls),
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py", line 280, in urls
return self.get_urls(), 'admin', self.name
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py", line 264, in get_urls
url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
AttributeError: 'Personal' object has no attribute 'urls'
我正在解释下面的代码。
models.py:
from __future__ import unicode_literals
from django.db import models
from django.contrib import admin
from datetime import datetime
class Service(models.Model):
"""In this class the columns for service table has declared"""
cname = models.CharField(max_length=200)
date_of_service = models.DateTimeField(default=datetime.now, blank=True)
vechile_no = models.CharField(max_length=200)
service_type = models.CharField(max_length=200)
class Personal(models.Model):
"""In this class the columns for Person table has declared"""
name = models.CharField(max_length=200)
address = models.CharField(max_length=200)
phone = models.CharField(max_length=15)
driving_license = models.CharField(max_length=200)
email = models.CharField(max_length=200)
date = models.DateTimeField(default=datetime.now, blank=True)
admin.site.register(Service, Personal)
urls.py:
from django.conf.urls import url, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
这里我需要在管理面板中显示这两个类。请帮我解决这个问题。
答案 0 :(得分:1)
这是问题所在:
admin.site.register(Service, Personal)
上述调用的第二个参数应该是ModelAdmin子类,而是传递Personal模型。其中自然没有urls属性。
AdminSite.register(model_or_iterable,admin_class = None, **选项)
使用给定的admin_class注册给定的模型类(或可迭代的类)。 admin_class默认为ModelAdmin
这样的代码应该按照惯例而不是models.py放在admin.py中,它应该看起来像
admin.site.register(Personal)
admin.site.register(Service)
如果您想使用自己的自定义ModelAdmin
class ServiceAdmin(admin.ModelAdmin):
list_fields = ['cname','date_of_service']
admin.site.register(Service, ServiceAdmin)