[BangPypers] How to iterate through columns in Model

kracekumar ramaraju kracethekingmaker at gmail.com
Thu Jul 16 22:23:56 CEST 2015


Hi Sunil

 Looks like you're using Django Here is an example

```
In [38]: user = User.objects.all()[0]

In [39]: fields = user._meta.local_fields

In [40]: for field in fields:
    if field.help_text:
        print(field.name, unicode(field.help_text), getattr(user, field.name
))
   ....:
('is_superuser', u'Designates that this user has all permissions without
explicitly assigning them.', True)
('username', u'Required. 30 characters or fewer. Letters, digits and
@/./+/-/_ only.', u'krace')
('is_staff', u'Designates whether the user can log into this admin site.',
True)
('is_active', u'Designates whether this user should be treated as active.
Unselect this instead of deleting accounts.', True)
```

On Fri, Jul 17, 2015 at 12:58 AM, Senthil Kumaran <senthil at uthcode.com>
wrote:

> On Thu, Jul 16, 2015 at 10:51 AM, Sunil Gupta <sunil at planmytour.in> wrote:
>
> > for each column in Obj
> >      if column.help_text != "" :
> >               print "The column ", column.verbose_name, " has value ",
> > column.value
> >
> > expected output
> >
> > The column Client Id has value Test_Client_Id
> >
>
>
> Look at the attributes / methods of Obj. Does it have something like
> .values() ? Check the help of your ORM to find out.
> then, it will be similar to what you have done.
>
> for column in Obj.values():
>    if column.help_text is not None:
>       print column.verbose_name
>
>
> What ORM are you using?
>
> --
> Senthil
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>



-- 

*Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
Torvaldshttp://kracekumar.com <http://kracekumar.com>*


More information about the BangPypers mailing list