6

How to access field in extended flatpage in django?
I wrote this:

class ExtendedFlatPage(FlatPage):
     teaser = CharField(max_length=150)

class ExtendedFlatPageForm(FlatpageForm):
     teaser = CharField(max_length=150)
     class Meta:
        model = ExtendedFlatPage

class ExtendedFlatPageAdmin(FlatPageAdmin):
    form = ExtendedFlatPageForm
    fieldsets = (
        (None, {'fields': ('url', 'title', 'teaser', 'content', 'sites',)}),
    )     

admin.site.unregister(FlatPage)
admin.site.register(ExtendedFlatPage, ExtendedFlatPageAdmin)

And creation in admin is ok. But then in flatpages/default.html I tried this:

<html>
<body>
<h1>{{ flatpage.title }}</h1>
<strong>{{ flatpage.teaser }}</strong>
<p>{{ flatpage.content }}</p>
</body>
</html>

And there was no flatpage.teaser! What is wrong?

Stan
  • 4,169
  • 2
  • 31
  • 39
  • 3
    I think the FlatpageFallbackMiddleware will still use default Flatpage model. Also your approach would use [Multi-table inheritance](http://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance) which means you have two tables created for extended model which makes your queries less efficient. – Davor Lucic Dec 22 '10 at 09:56
  • Yes, I saw second table and right value in it. The question is how can I access to new field in flatpage template? – Stan Dec 22 '10 at 09:58
  • 1
    You could create custom middleware which would use extended model instead of plain FlatPage. Take a look at FlatpageFallbackMiddleware code, it quite easy to modify to suite you own needs. – Davor Lucic Dec 22 '10 at 10:03

2 Answers2

6

Yes, as rebus mentioned the FlatpageFallbackMiddleware will pass default FlatPage model instance to the template. But in your case template variable {{ flatpage }} will also remember if it's ExtendedFlatPage instance, as described here in django-docs.

So to treat your flatpage as ExtendedFlatPage you have to use:

{{ flatpage.extendedflatpage.teaser }} instead of {{ flatpage.teaser }}.

Dzejkob
  • 2,302
  • 2
  • 15
  • 20
0

Sure you are passing the right object to the template, and not a plain FlatPage?

Also, unless your code paste is from different files and you've not shown your imports, your Model should have fields from django.models and your Form from django.forms. You're not showing the form in your little template so I don't think that's the problem... But if your model 'teaser' is coming from django.forms.CharField instead of django.models.CharField then... you probably won't see it. But you say its okay in the Admin?

Are you sure its rendering that template and not a similar one? (Yours should at least have an empty <blockquote> pair in it).

Try from the django shell, get your object (foo = ExtendedFlatPage.objects.get(id=1) or similar) and do foo.teaser on it...

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • He's using the middleware for flatpages so he's not actually passing an object, the middleware gets tthe default flatpages object as he hasn't told it to use the extended flatpage object. – Rasiel Dec 22 '10 at 22:36