On Jul 4, 12:01 pm, "David.D" <dengyuanzh...@gmail.com> wrote:
> i mean
> i have a model instance m, and m has a field fd.
> so i can wrote like this in template
>    {{ m.fd }}
>
> but, when i got a string  s = "fd"
>
> how could i do the same as above?
>
> thanks for any help...

There's no built-in way. A simple custom filter will do the job.

    @register.filter
    def field_from_string(instance, fieldname):
        return getattr(instance, fieldname)

Now you can do:

    {{ m|field_from_string:s }}

--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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