2016-11-27 96 views
1

我剛剛寫完了我的模型,我的英雄應用:Django的AttributeError的:「別名」對象有沒有屬性「的網址」

這裏是我的英雄我的應用程序文件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) 

我調整我的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) 

在命令提示符我輸入了:蟒蛇manage.py makemigrations,我得到這個錯誤 - 「AttributeError的:‘別名’對象有沒有屬性‘的網址’ 「:

(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上的其他類似問題,但我不明白如何解決它。

+1

是否'Alias'模型模型'ForeignKey'爲''的狀態,如果是。你想在管理中創建內聯嗎? –

+0

「register」的第二個參數應該是'ModelAdmin'的子類,而不是'Model'的子類! – schwobaseggl

+0

我不太明白你在說什麼,schwobaseggl。你指的是Admin.py文件嗎?哪個是第二個參數? –

回答

10

我終於在代碼欄的聚會上得到了我的答案!

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) 
+0

謝謝! :) – MrVocabulary

+0

或者你可以一次通過所有的模型,像這樣的數組參數: admin.site.register([Hero,Stats,Team,Status,Alias]) –

相關問題