I've just finished writing out my model for my Heroes App:
Here is my models.py file for my Heroes app:
from django.db import models
# 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)
I adjusted my settings.py file under the Installed Apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#custom apps
'heroes',
]
And then I adjusted my admin.py file:
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)
In the Command Prompt I typed out: python manage.py makemigrations and I got this error - "AttributeError: 'Alias' object has no attribute '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'
What does it mean and how do I fix it? I've looked at other similar problems on StackOverFlow but I didn't understand how to fix it.
I finally got my answer at a meetup at codebar!
Each class from model.p needs to be in a seperate line on admin.py.
So the answer is:
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)
You should change
admin.site.register(Status, Alias)
to
admin.site.register(Status)
admin.site.register(Alias)
This models should be added in admin by separate lines.
The register function takes a list of classes as its first parameter
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] )
(tl;dr: Put the models inside a list)
Had the same problem, found the solution here then read the code myself and thought I'd add some detail.
Here's the definition of the register function (from django/contrib/admin/sites.py).
def register(self, model_or_iterable, admin_class=None, **options):
...
It takes either a model or or an iterable, and the first thing it does is check whether it got a model, and if so, puts it inside a list, then runs the main loop on it (first line isn't relevant to that):
admin_class = admin_class or ModelAdmin
if isinstance(model_or_iterable, ModelBase):
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
...
When you put a second model not inside a list it gets passed to the admin_class parameter and treated as such until finally giving an error somewhere down the line.
So instead of calling this function multiple times you can just put all the models inside a list. I would do it even with one - the function is going to do it anyway, so you're just saving yourself and others who'll edit the code later from encountering this problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.