Here is the documentation on F() expressions:
http://docs.djangoproject.com/en/dev/topics/db/queries/#query-expressions
.  Basically, they are a way for you to refer to a model's own fields
in a query.

As for win_percentage being a property on MyModel, rather than a field
- yes, that would work well for this case.  However, this is just a
simplified example, to highlight what looks to me like a problem with F
() expressions, without cluttering it with a bunch of extraneous
stuff.

On Sep 15, 9:10 pm, Stefan Petrea <stefan.pet...@gmail.com> wrote:
> What are F() expressions and where are they documented ? (just didn't
> hear the term until now)
>
> About your win_percentage column , wouldn't you rather have it as a
> method which is being
> calculated from the columns wins and losses instead of a column stored
> inside the table ?
>
> On Sep 15, 8:36 pm, Brent Hagany <brent.hag...@gmail.com> wrote:
>
> > I'm having some trouble getting F() expressions to obey my parentheses
> > when I don't want the default order of operations.  For example, given
> > the model:
>
> > class MyModel(models.Model):
> >     wins = models.DecimalField(max_digits=1, decimal_places=0)
> >     losses = models.DecimalField(max_digits=1, decimal_places=0)
> >     win_percentage = models.DecimalField(max_digits=4,
> > decimal_places=3, default=Decimal('0.000'))
>
> > I get the following results when trying to calculate the
> > win_percentage:
>
> > In [1]: MyModel.objects.create(wins=2, losses=4)
> > Out[1]: <MyModel: MyModel object>
>
> > In [2]: MyModel.objects.all().update(win_percentage=F('wins') / (F
> > ('wins') + F('losses')))
> > Out[2]: 1
>
> > # I expect this to return Decimal("0.333")
> > In [3]: MyModel.objects.get(pk=1).win_percentage
> > Out[3]: Decimal("5.000")
>
> > It appears to be ignoring the parentheses around F('wins') + F
> > ('losses'), and so instead of 2 / (2 + 4) = .333, I'm getting 2 / 2 +
> > 4 = 5.  Am I doing this wrong, or is this by design, or is it a bug?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to