I noticed while looking for material for 
<https://code.djangoproject.com/ticket/20877> that 
<https://docs.djangoproject.com/en/dev/models/instances.html#updating-attributes-based-on-existing-fields/>
 mentions that:

    product.number_sold = F('number_sold') + 1

is faster than:

    product.number_sold += 1
 
though this doesn't seem to be mentioned in the database optimisation page.

That's easy enough to address, and 
<https://docs.djangoproject.com/en/dev/topics/db/optimization.html#do-database-work-in-the-database-rather-than-in-python/>
 seems like a sensible place for it.

However the mentions of F() that I did find raised a number of questions.

The F() class seems to be a handy general-purpose way to refer to the value of 
a model field..


Firstly, it's not explained how, in expressions like:

    product.number_sold = F('number_sold') + 1

(from 
<https://docs.djangoproject.com/en/dev/models/instances.html#updating-attributes-based-on-existing-fields/>)
 Django knows that F('number_sold') is refers to the product model. 

Does it know because product.number_sold is the field that this expression 
refers to? What would happen if we did:

    product.number_in_stock = F('number_in_stock') - F('number_sold)

(i.e. can we make such calculations multiple other fields in one go?), or:

    product.number_to_reorder = F('number_sold)

for example? What are the rules of the usage syntax of F()?

Secondly, the main documentation for F() 
<https://docs.djangoproject.com/en/dev/topics/db/queries.html#query-expressions/>
 doesn't mention this kind of use at all: it only suggests that it might be 
useful in queries.

Since this use seems to be just one of multiple uses for F(), shouldn't a more 
general description of F() belong somewhere else (where) instead?


Finally, are there any other useful ways to use F() not covered by these two 
examples?


Daniele

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to