python 2.7 - Display django Model fields in a table -


i have model 30+ fields , want display 20 of them in table. know there {{ form.as_table }} there comparable function non-form models? using

{% name, value in article.get_fields %}      <tr>      {% if value %}         <td>{{ name }} = {{ value }}</td>       {% endif %}      </tr> {% endfor %} 

-where get_fields returns fields of article. works fine. guess there built-in django function doing same thing, can't find in documentation.

i agree @kathikr, there isn't function built in model class you.

one option subclass model , add as_table() function parsed available fields , used exclude attribute necessary.

    def as_table(self):             return "".join(["<tr><td>%s</td><td>%s</td></tr>" % (field.verbose_name, field.value_to_string(self)) field in self._meta.fields if field not in self.exclude]) 

hmm, probable shouldn't one-liner

    def as_table(self):         result = ""         field in self._meta.fields:             if field not in self.exclude:                 result += "<tr><td>%s</td><td>%s</td></tr>" %                      (field.verbose_name, field.value_to_string(self))         return result 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -