Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django AttributeError: 'Alias' object has no attribute 'urls'

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.

like image 324
Wadzanai Mufunde Avatar asked Nov 27 '16 20:11

Wadzanai Mufunde


Video Answer


1 Answers

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)
like image 150
Wadzanai Mufunde Avatar answered Oct 16 '22 18:10

Wadzanai Mufunde