Django F field iteration
<p>I created a simple Django model to explore F field but could iterate over the field.</p>

<pre><code>class PostgreSQLModel(models.Model):
    class Meta:
        abstract = True
        required_db_vendor = 'postgresql'

class NullableIntegerArrayModel(PostgreSQLModel):
    field = ArrayField(models.IntegerField(), blank=True, null=True)    
</code></pre>

<p>Now, from my django shell I created a F object as below.Not sure what this object contains. Does it contain all the ids? How can I iterate over the result?</p>

<pre><code>&gt;&gt;&gt; a=F('id')
&gt;&gt;&gt; a
F(id)
&gt;&gt;&gt; dir(a)
['ADD', 'BITAND', 'BITOR', 'DIV', 'MOD', 'MUL', 'POW', 'SUB', '__add__', '__and__', '__class__', '__delattr__', '__dict__', '__dir__', '__div__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__or__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__weakref__', '_combine', 'asc', 'bitand', 'bitor', 'desc', 'name', 'resolve_expression']
</code></pre>

<p>F is not a field.</p>

<blockquote>
  <p>An F() object represents the value of a model field or annotated column. It makes it possible to refer to model field values and perform database operations using them without actually having to pull them out of the database into Python memory. - <a href="https://docs.djangoproject.com/en/1.10/ref/models/expressions/#f-expressions" rel="nofollow">F() expressions</a></p>
</blockquote>

<p>In summary, you use <code>F()</code> objects whenever you need to reference another field's value in your queries. By itself, <code>F()</code> objects doesn't mean anything. They're used to reference a field value on the same queryset. </p>

<p>For example (a very simple example), in your model, if you want to query objects where <code>field</code> value is twice its <code>id</code>, you would need to <a href="https://docs.djangoproject.com/en/1.10/topics/db/queries/#filters-can-reference-fields-on-the-model" rel="nofollow">reference <code>id</code> field's value</a> while filtering, so you could use <code>F()</code> like this:</p>

<pre><code>NullableIntegerArrayModel.objects.filter(field=F('id') *2)
</code></pre>

<p><code>F('id')</code> simply references the <code>id</code> value for that model. Django uses it to create corresponding SQL statement. In this case something like this:</p>

<pre><code>'SELECT "app_model"."id", "app_model"."field" FROM "app_model" 
 WHERE "app_model"."field" = (("app_model"."id" * 2))'
</code></pre>

<p>Without <code>F()</code> expressions you would either write your raw SQL or do the filtering in Python (which reduces the performance especially when there are lots of objects). </p>

<hr>

<p>References:</p>

<ul>
<li><a href="https://docs.djangoproject.com/en/1.10/topics/db/queries/#filters-can-reference-fields-on-the-model" rel="nofollow">Filters can reference fields on model</a></li>
<li><a href="https://docs.djangoproject.com/en/1.10/ref/models/expressions/#django.db.models.F" rel="nofollow">F expressions</a></li>
</ul>

<p>From <code>F()</code> class definition:</p>

<blockquote>
  <p>An object capable of resolving references to existing query objects. - <a href="https://docs.djangoproject.com/en/1.10/_modules/django/db/models/expressions/#F" rel="nofollow">F source</a></p>
</blockquote>

python django