Django AttributeError:' Alias'对象没有属性' urls'

时间:2016-11-27 20:04:36

标签: python django django-models migration

我刚刚为我的英雄应用程序写完了我的模型:

这是我的Heroes应用程序的models.py文件:

来自django.db导入模型

# Create your models here.
class Hero(models.Model):
    codename = models.CharField(max_length = 30)
    profilePic = models.ImageField(blank=True) #blank makes this optional

    def __str__(self):
        return (self.codename)

class Stats(models.Model):
    heroID = models.ForeignKey('Hero')
    height = models.CharField(max_length = 10)
    weight = models.CharField(max_length = 10)
    STATS_CHOICES = (
    ('1', 'Extremely Low'),
    ('2', 'Very Low'),
    ('3', 'Low'),
    ('4', 'Average'),
    ('5', 'Good'),
    ('6', 'Above Average'),
    ('7', 'High'),
    ('8', 'Very High'),
    ('9', 'Super Human'),
    ('10', 'Above and Beyond'))
    powers = models.CharField(max_length = 5, choices = STATS_CHOICES)
    intelligence = models.CharField(max_length = 5, choices = STATS_CHOICES)
    durability = models.CharField(max_length = 5, choices = STATS_CHOICES)
    strength = models.CharField(max_length = 5, choices = STATS_CHOICES)
    speed = models.CharField(max_length = 5, choices = STATS_CHOICES)

    def __str__(self):
        return (self.heroID)

class Team(models.Model):
    name = models.CharField(max_length = 25)
    leader = models.IntegerField
    address = models.TextField
    description = models.TextField
    members = models.TextField

class Status(models.Model):
    heroID = models.ForeignKey('Hero')
    missionID = models.IntegerField
    TeamID = models.IntegerField

    def __str__(self):
        return (self.heroID, self.missionID, self.TeamID)

class Alias(models.Model):
    heroID = models.ForeignKey('Hero')
    firstName = models.CharField(max_length = 25)
    surname = models.CharField(max_length = 25)
    formerCodeNames = models.TextField
    occupation = models.CharField(max_length = 30)
    address = models.TextField
    citizenship = models.CharField(max_length = 40)
    species = models.CharField(max_length = 40)

    def __str__(self):
        return (self.heroID, self.firstName, self.surname)

我调整了Installed Apps下的settings.py文件:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #custom apps
    'heroes',
]

然后我调整了我的admin.py文件:

from django.contrib import admin

# Register your models here.
from .models import Hero, Stats, Team, Status, Alias

admin.site.register(Hero, Stats)
admin.site.register(Team)
admin.site.register(Status, Alias)

在命令提示符中,我输入了:python manage.py makemigrations,我收到了此错误 - "属性错误:'别名'对象没有属性' urls'":

(secondproject) C:\Python34\Scripts\secondproject\heroes4Hire>python manage.py
emigrations
aceback (most recent call last):
File "manage.py", line 22, in <module>
  execute_from_command_line(sys.argv)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\managem
\__init__.py", line 367, in execute_from_command_line
  utility.execute()
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\managem
\__init__.py", line 359, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\managem
\base.py", line 294, in run_from_argv
  self.execute(*args, **cmd_options)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\managem
\base.py", line 342, in execute
  self.check()
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\managem
\base.py", line 374, in check
  include_deployment_checks=include_deployment_checks,
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\managem
\base.py", line 361, in _run_checks
  return checks.run_checks(**kwargs)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\checks\
istry.py", line 81, in run_checks
  new_errors = check(app_configs=app_configs)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\checks\
s.py", line 14, in check_url_config
  return check_resolver(resolver)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\core\checks\
s.py", line 24, in check_resolver
  for pattern in resolver.url_patterns:
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\utils\functi
l.py", line 35, in __get__
  res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\urls\resolve
py", line 313, in url_patterns
  patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\utils\functi
l.py", line 35, in __get__
  res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\urls\resolve
py", line 306, in urlconf_module
  return import_module(self.urlconf_name)
File "C:\Python34\Scripts\secondproject\lib\importlib\__init__.py", line 109,
 import_module
  return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Python34\Scripts\secondproject\heroes4Hire\heroes4Hire\urls.py", lin
0, in <module>
  url(r'^admin/', admin.site.urls),
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\contrib\admi
ites.py", line 267, in urls
  return self.get_urls(), 'admin', self.name
File "C:\Python34\Scripts\secondproject\lib\site-packages\django\contrib\admi
ites.py", line 251, in get_urls
  url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name), include(m
l_admin.urls)),
tributeError: 'Alias' object has no attribute 'urls'

这是什么意思,我该如何解决?我已经在StackOverFlow上查看了其他类似的问题,但我并不了解如何修复它。

3 个答案:

答案 0 :(得分:19)

我终于在代码栏的聚会上得到了答案!

来自model.p的每个类都需要在admin.py上单独排列。

所以答案是:

from django.contrib import admin

# Register your models here.
from .models import Hero, Stats, Team, Status, Alias

admin.site.register(Hero)
admin.site.register(Stats)
admin.site.register(Team)
admin.site.register(Status)
admin.site.register(Alias)

答案 1 :(得分:1)

您应该更改

admin.site.register(Status, Alias)

admin.site.register(Status)
admin.site.register(Alias)

此模型应在管理员行中单独添加。

答案 2 :(得分:0)

注册功能将类别列表作为其第一个参数

from django.contrib import admin

# Register your models here.
from .models import Hero, Stats, Team, Status, Alias

admin.site.register( [Hero, Stats, Team, Status, Alias] )