1

I am working on adding a Django model where there are a number of fields that I want created as a DecimalField, but I don't know the max_digits or decimal_places for each of these columns. I am using a postgres database; the table structure comes from and will be populated by an API source.

I was able to get DDL for the table, create the table in a blank postgres db, and use inspectdb to create a Django model.

The result assigned all DecimalField columns with: models.DecimalField(max_digits=65535, decimal_places=65535, blank=True, null=True)

The model contains ~170 DecimalField fields. Are there any ideas or suggestions on how I should handle the creation of these fields?

  • What is the definition for the numeric(decimal) fields in the database? See here [Numeric](https://www.postgresql.org/docs/12/datatype-numeric.html) and here [Usage](https://www.postgresql.org/docs/12/datatype-numeric.html) for numeric specifications in Postgres. What are the specifications for the values you are going to store? – Adrian Klaver Jul 31 '20 at 15:37
  • The postgres table that was created and used by inspectdb to create the class definition uses a numeric data type without any precision or scale. – Rory-R-Reyes Jul 31 '20 at 16:23
  • The table is a denormalized oneline summary type of table so the values are for numerous types of data – Rory-R-Reyes Jul 31 '20 at 16:37
  • Yeah, but it is the numeric/decimal ones you are concerned with here. If you go to the links I posted you will see what numeric fields can hold, in particular what that is when no scale or precision is specified. Basically the limits of the field. That should hold any data you want to input. Then you have determine whether the model specifications will cover the numeric/decimal data you are adding. – Adrian Klaver Jul 31 '20 at 16:59
  • Okay, I understand what you are saying. Use the maximum precision and scale allowed for a non-specified precision or scale numeric postgres data type. By doing this, I guarantee that all data will be handled in the model – Rory-R-Reyes Jul 31 '20 at 18:41
  • Please read the links I sent. If you don't specify the values in the numeric() field you will get the default values as stated in the link docs. All data up to ```max_digits=65535, decimal_places=65535```, though the ```decimal_places``` is not really accurate. Nor is ```max_digits```. – Adrian Klaver Jul 31 '20 at 18:57

0 Answers0