Monday, October 17, 2011

Re: Automaticall list all fields of generic model in template

Did either of you read the OPs email? He is not looking for forms...

OP:

There is nothing built in, but you can easily iterate through a
model's fields, they are all available in the _meta attribute on a
model instance:

>>> u = User.objects.get(id=1)
>>> for field in u._meta.fields:
... print "%s => %s" % (field.name, getattr(u, field.name))
...
id => 1
username => tevans@...
first_name => Tom
last_name => Evans
email => tevans@...
is_staff => True
is_active => True
is_superuser => True
last_login => 2011-10-17 15:46:33
date_joined => 2008-11-10 10:54:33

Using this information, you can easily produce a method that will
output a HTML table of that data. If you can modify your model
classes, you can make them inherit from an abstract base model that
includes this method, and then it will be available in all your
classes (that inherit from the base class, obviously).

Much more hackily, you could monkey patch models.Model instead.

Cheers

Tom

On Mon, Oct 17, 2011 at 2:43 PM, Andre Terra <andreterra@gmail.com> wrote:
> Even better, use django-form-utils!
>
> https://bitbucket.org/carljm/django-form-utils/overview
>
>
> Cheers,
> AT
>
> On Sun, Oct 16, 2011 at 7:57 PM, Mario Gudelj <mario.gudelj@gmail.com>
> wrote:
>>
>> I think you need
>> this https://docs.djangoproject.com/en/dev/topics/forms/modelforms/
>>
>> On 16 October 2011 18:24, bovender <dr.daniel.kraus@googlemail.com> wrote:
>>>
>>> Hi all,
>>> new to Django and loving it.
>>> Is there a way to automatically generate HTML output for the fields of a
>>> model instance?
>>> I'm looking for something similar to form.as_table, just without the form
>>> widgets.
>>> Currently I find myself manually repeating all the typing work for the
>>> detail view templates that I already did for the model definitions.
>>> Thanks
>>> bovender
>>>
>>> --
>>> 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/-/iGP654BL2HMJ.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> django-users+unsubscribe@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+unsubscribe@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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate