I just used the following code on a project to solve the problem you're
describing:

models.py
class SomeModel(models.Model):
    ...
    def get_fields(self):
        return [(field.name, field.value_to_string(self)) for field in
SomeModel._meta.fields if field.value_to_string(self) is not None]

It's a basic extension to the link that Kurtis just provided.

HTH.

On Thu, Oct 11, 2012 at 1:33 PM, Kurtis Mullins <kurtis.mull...@gmail.com>wrote:

> There's a couple of ways, assuming you're talking about in the template.
>
> One example would simply be:
>
> {% if object.fieldname %}
> {{ object.fieldname }}
> {% endif %}
>
> Another example might be:
>
> {{ object.fieldname|default:"" }}
>
> But it doesn't allow for much formatting of an empty field.
>
> If you want to do this generically, you could take an approach of looping
> through all fields of a model and checking if each has a value before
> printing. Something along the lines of this might work for you:
>
>
> http://stackoverflow.com/questions/2217478/django-templates-loop-through-and-print-all-available-properties-of-an-object
>
> Good luck!
>
> On Thu, Oct 11, 2012 at 1:47 PM, Keir Lawson <keirlaw...@gmail.com> wrote:
>
>> Hello,
>>
>> I was wondering what the most elegant way (using a DetailView or similar)
>> to display the fields of a model, excluding those that are empty?
>>
>> Thanks
>>
>> Keir
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/django-users/-/uYV-psryrHcJ.
>> To post to this group, send email to django-users@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>



-- 
Jonathan D. Baker
Developer
http://jonathandbaker.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to