<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Django foo &#187; get fields</title>
	<atom:link href="http://www.djangofoo.com/tag/get-fields/feed" rel="self" type="application/rss+xml" />
	<link>http://www.djangofoo.com</link>
	<description>Django Tips and Tweaks</description>
	<lastBuildDate>Wed, 18 Aug 2010 09:38:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Get/List model fields</title>
		<link>http://www.djangofoo.com/80/get-list-model-fields</link>
		<comments>http://www.djangofoo.com/80/get-list-model-fields#comments</comments>
		<pubDate>Mon, 22 Feb 2010 14:39:11 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[fields]]></category>
		<category><![CDATA[get fields]]></category>
		<category><![CDATA[list fields]]></category>
		<category><![CDATA[meta fields]]></category>
		<category><![CDATA[model]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=80</guid>
		<description><![CDATA[It is possible to get/list all of the fields for the given model, to do so you need to access the _meta attribute of the Model. The following example demonstrates how to do it. # in the models.py class Car(models.Model) year = models.IntegerField() name = models.CharField(max_length=50) ... def get_fields(self): # make a list of field/values. [...]]]></description>
			<content:encoded><![CDATA[<p>It is possible to get/list all of the fields for the given model, to do so you need to access the <strong>_meta</strong> attribute of the Model.<br />
The following example demonstrates how to do it.</p>
<pre class="brush: python">

# in the models.py
class Car(models.Model)
    year = models.IntegerField()
    name = models.CharField(max_length=50)
    ...

    def get_fields(self):
        # make a list of field/values.
        return [(field, field.value_to_string(self)) for field in Car._meta.fields]
</pre>
<p>Now that we implemented the get_fields function for our Car model, we can use this in the template&#8230;</p>
<pre class="brush: python">
# your template file...
# I assume that the variable 'car' is an instance of Car object.
{% for field, value in car.get_fields %}
    {{ field }} : {{ value }}
{% endfor %}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/80/get-list-model-fields/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

