Steps to reproduce:
- Setup a legacy db in settings.py (see https://docs.djangoproject.com/en/2.0/howto/legacy-databases/)
- Prerequisite: you have a column in one of your tables that has a default value.
- Example: test_field int(11) DEFAULT 1
- Run python manage.py inspectdb [--database DATABASE_NAME [ TABLE ] ] > models.py
- Open models.py
- If you don't include > models.py you can see the output from the commandline
- test_field will look something like this:
- test_field = models.IntegerField(blank=True, null=True)
- What I would expect:
- test_field = models.IntegerField(default=1)
What I've been able to dig up:
# from
inspectdb.py (line 144, comments included in the original file)
142 # Add 'null' and 'blank', if the 'null_ok' flag was present in the
143 # table description.
144 if row[6]: # If it's NULL...
145 extra_params['blank'] = True
146 extra_params['null'] = True
86 fields = []
87 for line in cursor.description:
88 info = field_info[line[0]]
89 fields.append(FieldInfo(
90 *line[:3],
91 to_int(info.max_len) or line[3],
92 to_int(info.num_prec) or line[4],
93 to_int(info.num_scale) or line[5],
94 line[6],
95 info.column_default, # THIS LINE IS JUST WHATEVER THE COLUMN DEFAULT IS
96 info.extra,
97 info.is_unsigned,
98 ))
99 return fields
I'm sure there are a lot of things I don't understand about this, but I don't think that having a default value should automatically add blank=True, null=True.
Is having a default value really a "'null_ok' flag"?
I think it should only put blank=True, null=True if the default is set to NULL in the column. Otherwise, it should set the default to whatever the default value is (ex: default=1)
Caveat: I've only tested this with int fields.