Listing all the fields in a Django model


written by stderr on May 14, 2009 - filed under python, django, webdev

I skimmed through the documentation, looking for a way to iterate over the specific fields in a Django model. Seems pretty elementary, right? Google wasn't any help, so I looked into django.db.base.py.

And voila! Just like that, I found what I was looking for:

>>> Post._meta.fields
[<django.db.models.fields.AutoField object at 0x86a20cc>,
 <django.db.models.fields.CharField object at 0x869da8c>,
 <django.db.models.fields.TextField object at 0x869db0c>, 
<django.db.models.fields.DateTimeField object at 0x869db2c>,
<django.db.models.fields.related.ForeignKey object at 0x869db4c>, 
<django.db.models.fields.SlugField object at 0x869db6c>]
>>> for f in Post._meta.fields:
...   print f.attname
... 
id
title
body
written_at
author_id
slug
Obviously, very straightforward.

Well, mostly. To access many-to-many fields, you have to use:

>>> Post._meta.many_to_many
[<django.db.models.fields.related.ManyToManyField object at 0x869dc0c>]
There's also a virtual_fields list, which I assume has something to do with generic relations and the like:
>>> Post._meta.virtual_fields
[]

Certainly better than the god-awful hack I found from a couple years ago. *shiver*

      At 12:05 AM on July 2nd, 2009, BaKiNeC commented...

Я тоже иногда такое замечал, но как-то ранее не придавал этому никакого значения.

      At 4:21 PM on June 29th, 2009, VordEndofop commented...

Требуются торговые представители по продаже фильтров для воды. Комиссия от 21 до 51 %. Развиваем сеть представителей по России и странам СНГ. От Вас резюме на gold.formula.water@gmail.com

      At 10 AM on May 30th, 2009, Игорь commented...

Познавательно. Чувствуется позитив :)

      At 11:09 AM on May 21st, 2009, Ubay Oramas commented...

Thanks for the tip. Helped a lot. Best regards.







       



Develop.Github!


written by stderr on Apr 14, 2009 - filed under python, ruby, truedat, webdev

*Droooooooooooooooooooooooooooooooooooool*

Gonna play with this tonight if I don't collapse into a sick pile of useless coder. Running a fever and horrific sinus pressure is no way to go through life, Scout.

      At 4:37 PM on July 3rd, 2009, pharmacy commented...

http://ddtuning.com/forums/viewtopic.php?t=7&sid=88702404b109eb946909f543beb86b50 [u][b]>>> BUY GENERIC PROPECIA $0.51 per pill BUY ONLINE WITHOUT PRESCRIPTION <<< [/b][/u] [center][url=http://www.cheap-propecia.pharmacy-online-store.net][img]http://www.propecia.freehostp.com/propecia-generic.jpg[/img][/url] [b]Propecia generic propecia[/b] [url=http://www.cheap-propecia.pharmacy-online-store.net][img]http://www.propecia.freehostp.com/generic-propecia-online.jpg[/img][/url] [b]buy propecia does propecia work [/b] [url=http://www.cheap-propecia.pharmacy-online-store.net][img]http://www.propecia.freehostp.com/generic-propecia.jpg[/img][/url][/center] Propecia generic propecia propecia side effects does propecia really work cheap propecia buy propecia does propecia work comprar propecia lamina propecia propecia generica propecia without prescription [b]propecia hair loss pattern[/b] [url=http://www.gnaas.com/test/showthread.php?p=578]Propecia[/url] - lamina propecia [b]propecia photos[/b] [url=http://www.gnaas.com/test/member.php?u=30]hair loss treatment propecia[/url] - propecia taking more than 1mg [b]does propecia work[/b]

      At 7:03 AM on July 3rd, 2009, pharmacy commented...

<center><a href=http://www.newviagraforum.medsjoy.biz><img>http://www.blogs.medextreme.com/image/buying_viagra.jpg</img></a><a href=http://www.newviagraforum.medsjoy.biz><img>http://www.blogs.medextreme.com/image/buying_levitra.jpg</img></a><a href=http://www.newviagraforum.medsjoy.biz><img>http://www.blogs.medextreme.com/image/buying_cialis.jpg</img></a></center> <u><b>CHEAP CIALIS ONLINE BUYING $1.55 per pill </b></u>low price GENERIC CIALIS BUY CHEAP GENERIC CIALIC BUY online now http://1matters.org/phpbb/viewtopic.php?t=82&postdays=0&postorder=asc&start=0&sid=3dd3de06fdf43a45978120f2c91d2ec4 <b>GENERIC CIALIS money BUY</b> <a href=http://atipc.homelinux.org/xoops22/modules/newbb/viewtopic.php?topic_id=1641>GENERIC CIALIS BUY online</a> - BUY sialis <b>GENERIC CIALIS money BUY</b> http://hacksmeoff.com/showthread.php?s=de8ed32b5e7d3a5f4d4ba34c1204278d&t=104 <b>BUY GENERIC CIALIS GENERIC CIALIS online</b>







       



He is a helpful little fellow


written by stderr on Apr 07, 2009 - filed under django, truedat, webdev, humor

I'd briefly seen the Django critters thread in passing, whilst perusing the Django Users group.

But after sitting down and reading it, I came across a pretty great story, I suggest you read it. Better yet: one of the images, rendered by Andrew McCloud, is spectacular and particularly Django-esque. So, with permission from the artist, I present to you, the new truedat.org 404. It's purty.

Thanks, Andrew!








       



Pyccuracy? Pyawesome!


written by stderr on Apr 06, 2009 - filed under python, ruby, truedat

Wow. I wondered when this would happen. Expect Pyccuracy tests for truedat.org.

Is it savvier than Cucumber? Probably not. Yet.








       



Sitemaps added


written by stderr on Apr 06, 2009 - filed under django, truedat, webdev

Voila!. Sitemaps have been added.

      At 1:59 PM on July 3rd, 2009, Debygitte commented...

6261<a href="http://cgi3.ebay.fr/ws/eBayISAPI.dll?ViewUserPage&userid=1euro-viagra">viagra</a> en ligne! viagra en ligne. commander viagra en ligne Achat viagra en ligne dans notre pharmacie. :-] <a href=http://cgi3.ebay.fr/ws/eBayISAPI.dll?ViewUserPage&userid=1euro-viagra>viagra belgique soft generique</a>

      At 12:23 PM on April 14th, 2009, stderr commented...

Actually, I just used Django's fantastic sitemap framework. Documentation is available here: http://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/. But, that being said, I'd certainly like to take a look at your code. :). The more the merrier!

      At 11:33 AM on April 10th, 2009, Hugh Brown commented...

How did you generate the sitemap.xml -- by hand? If you are interested, I wrote python code to generate the sitemap by walking the directory tree.