On Jul 4, 9:28 pm, TechnicalBard <[email protected]> wrote:
> But the admin app does - it defines the labels from the attribute
> names and just puts them all in a table or list.
>
> How does one do that?
Actually, you tell it which fields you want to display through the
list_display attribute on your Admin class.
Analogous to iterating through a list of
> objects, is it possible to iterate through the attributes of a single
> object?
The _meta object of a model instance has a method that can give you
all the field names:
your_model_instance._meta.get_all_field_names()
There are several other methods and properties like this available on
the _meta[1] object (for example, get_field(), .fields,..) Once you
learn what they do, you will be able to deduce the field names you
need and then call getattr(your_model_instance, field_name) on them.
You will probably also want to distinguish between local fields and
related fields such as foreign keys and m2m fields. You can't do much
of this in the template. You will have to do all this introspection in
your view and then pass on suitable context variables to your template
which can be fairly generic.
Martin Alchin's book Pro Django[2] is a must-read if you want to dive
into this kinda of stuff.
[1]
http://code.djangoproject.com/browser/django/trunk/django/db/models/options.py
[2] http://www.amazon.com/Pro-Django-Experts-Voice-Development/dp/1430210478
-RD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---