Opened 16 years ago

Closed 16 years ago

#6688 closed (invalid)

Methods of fieldless model can't be called in templates

Reported by: sam_cel Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm trying to write a wiki-like app and use SVN revision 7186.

models.py:

class Article(Model):
	class Admin:
		pass
	def __unicode__(self):
		return str(self.pk)
	def get_absolute_url(self):
		return "/pages/" + self.pk + "/"

class ArticleEdit(Model):
	author = ForeignKey(User, related_name = "contributions")
	[...all other data...]
	article = ForeignKey(Article, related_name = "edits")

views.py:

def index(request):
        article = Article.objects.all()[0]
        return render_to_response("index.html", { "article" : article, })

The Article model is just used to tie the edits together.
I ran into two problems:

1) when I'm passing an Article-object to a template, {{ article.pk }} always returns 1 and {{ article.get_absolute_url }} doesn't exist.

2) http://code.djangoproject.com/ticket/1972
That ticket was WONTFIXed, but obviously people keep running into this problem. I can't think of any clean way to model the above scenario with django, so maybe ticket 1972 should be reconsidered as well.

Change History (1)

comment:1 by Malcolm Tredinnick, 16 years ago

Resolution: invalid
Status: newclosed

Questions like this are probably more appropriate for the django-users list, since there's a fair chance it's going to be problems with your code. There are no bugs here.

  1. article.pk is always the same because you're always selecting the same object.
  2. get_absolute_url() contains an error (you're concatenating strings and ints), so the template will return nothing when access it.
  3. Asking for #1972 to be reconsidered is not done by opening another ticket. Please, read contributing.txt for details on how to have a wontfix'ed ticket reconsidered.
Note: See TracTickets for help on using tickets.
Back to Top