| 179 | |
| 180 | == Renamed !FloatField to !DecimalField == |
| 181 | |
| 182 | In [5302], we fixed a slight inconsistency in Django's model field system. Previously, we had a !FloatField that had a maximum number of digits and decimal places. However, although it was stored in the database as a fixed precision value, it was stored in Python as a float, so precision was lost due to rounding problems. |
| 183 | |
| 184 | We now have a !DecimalField class that is the same as the old !FloatField, except that it is represented in Python by a Decimal type (and still stored as a fixed precision value in the database). We have also introduced a proper !FloatField that is represented as a float in Python and stored as a "double precision" field in the database. |
| 185 | |
| 186 | To make your code compatible with these changes, you need to change all occurrences of !FloatField in your code to !DecimalField. You only need to rename the field type -- all the parameters are the same. So change |
| 187 | {{{ |
| 188 | #!python |
| 189 | class MyModel(models.Model): |
| 190 | ... |
| 191 | field_name = models.FloatField(max_digits=10, decimal_places=3) # old code! |
| 192 | }}} |
| 193 | to |
| 194 | {{{ |
| 195 | #!python |
| 196 | class MyModel(models.Model): |
| 197 | ... |
| 198 | field_name = models.DecimalField(max_digits=10, decimal_places=3) # new code! |
| 199 | }}} |
| 200 | |
| 201 | If you forget to make this change, you will see errors about !FloatField not taking a max_digits attribute in {{{__init__}}}, since the new !FloatField takes no precision-related arguments. |
| 202 | |
| 203 | If you are using MySQL or PostgreSQL, there are '''no further changes are needed'''. The database column types for !DecimalField are the same as for the old !FloatField. |
| 204 | |
| 205 | If you are using SQLite, you need to force the database to view the appropriate columns as decimal types, rather than floats. To do this, follow this procedure for every application you have that contains a !DecimalField. Do this ''after'' you have made the change to using !DecimalField in your code and updated the Django code. |
| 206 | |
| 207 | '''Warning: Back up your database first! ''' For SQLite, this means making a copy of the single file that stores the database (the name of that file is the DATABASE_NAME in your settings.py file). |
| 208 | |
| 209 | For every application using a !DecimalField, do the following. We will use applications call {{{some_app}}} and {{{another_app}}} in this example |
| 210 | {{{ |
| 211 | ./manage.py dumpdata --format=xml some_app another_app > data-dump.xml |
| 212 | ./manage.py reset some_app another_app |
| 213 | ./manage.py loaddata data-dump.xml |
| 214 | }}} |
| 215 | |
| 216 | Notes: |
| 217 | 1. It is important that you remember to use XML format in the first step of this process. We are exploiting a feature of the XML data dumps that makes porting floats to decimals with SQLite possible. |
| 218 | 2. In the second step you will be asked to confirm that you are prepared to lose the data for the application(s) in question. We restore this data in the third step, of course. |
| 219 | |
| 220 | If something goes wrong in the above process, just copy your backed up database file over the top of the original file and start again. |