'Account' object has no attribute 'get_absolute_url'
When attempting to access my sitemap.xml, I'm receiving this error:
'Account' object has no attribute 'get_absolute_url' on line 112.
109. def get_absolute_url(self):
110. if self.group is None:
111. return reverse('wiki_article', args=(self.title,))
112. return self.group.get_absolute_url() + 'wiki/'开发者_开发知识库 + self.title
I can't find this 'Account' object in the traceback. Am I failing to import something here? Please let me know if you need more information.
You have to define that method.
get_absolute_url
Model.get_absolute_url()
Define a get_absolute_url() method to tell Django how to calculate the canonical URL for an object. To callers, this method should appear to return a string that can be used to refer to the object over HTTP.
For example:
def get_absolute_url(self):
return "/people/%i/" % self.id
While this code is correct and simple, it may not be the most portable way to write this kind of method. The reverse() function is usually the best approach.
For example:
def get_absolute_url(self):
from django.core.urlresolvers import reverse
return reverse('people.views.details', args=[str(self.id)])
Reference: https://docs.djangoproject.com/en/1.9/ref/models/instances/
I've never used it, or this wiki app, but it simply sounds like your Account
model doesn't have a get_absolute_url
method.
http://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/#django.contrib.sitemaps.Sitemap.location
If location isn't provided, the framework will call the get_absolute_url() method on each object as returned by items().
Are you using this app? http://code.google.com/p/django-wikiapp/source/browse/trunk/wiki/models.py?r=161 (I just searched your traceback to find it)
Group
is a generic foreign key so it can point to any of your models, which means every model it points to must have a get_absolute_url
defined.
Update:
If you don't have an Account
model, I'd suggest searching for it in django.contrib.contenttypes.ContentType
because apparently an article is referencing it..
from django.contrib.contenttypes.models import ContentType
ContentType.objects.filter(model__icontains="account")
Do you get any results?
Update:
So you've found an 'account'
ContentType
.
Now you can get the class via contenttype.model_class()
and thus find it to implement get_absolute_url()
there or since it sounds like you're not actually using this class, you can find which Article
's are pointing to this mysterious account
ContentType by querying Article
by ContentType
.
content_type = ContentType.objects.get(model__icontains='account')
articles_pointing_to_account = Article.objects.filter(content_type__pk=content_type.pk)
# Now it's your choice what to do with these articles.
# I'd be curious what they are and how they managed to pull off this stunt
# before removing their generic relation to Account
精彩评论