Code

Opened 6 years ago

Closed 6 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: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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.

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.