## #11886 closed (fixed)

# F() expressions don't retain parentheses into final SQL

Reported by: | Russell Keith-Magee | Owned by: | nobody |
---|---|---|---|

Component: | Uncategorized | Version: | 1.1 |

Severity: | Keywords: | ||

Cc: | Triage Stage: | Unreviewed | |

Has patch: | no | Needs documentation: | |

Needs tests: | Patch needs improvement: | ||

Easy pickings: | UI/UX: |

### Description

Reported by Brent Hagany on django-users:

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.

### Change History (2)

### comment:1 Changed 7 years ago by

Resolution: | → fixed |
---|---|

Status: | new → closed |

### comment:2 Changed 7 years ago by

**Note:**See TracTickets for help on using tickets.

(In [11581]) Fixed #11886 -- Corrected handling of F() expressions that use parentheses. Thanks to Brent Hagany for the report.