How can i access the model instance used by a form from a template?
I am iterating over a formset made of modelforms in my template. I want to provide aditional information on that model. If the answer to this How to Access model from Form template in Django question would work, i could do this:
{% for form in formset.forms %}
Status:{{ form._meta.model.status }}
{{form}}
{%开发者_开发百科 endfor %}
But that just throws the TemplateSyntaxError: Variables and attributes may not begin with underscores.
I don't think that's what you want to do. A model is a class: it won't have a status
, as that's a field which only gets a value for a particular instance.
I suspect what you mean to do is access the model instance associated with the form, which is just form.instance
.
If you create a property on the form that reads the value then you can access it very easily in the template.
class SomeForm(...):
@property
def status(self):
return self._meta.model.status
...
{{ form.status }}
精彩评论