Opened 5 years ago

Closed 5 years ago

#30794 closed New feature (duplicate)

inspectdb should generate related_name for ForeignKey.

Reported by: mingwugmail Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: related_name
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

https://stackoverflow.com/a/52872876

the solution is here:

#Original inspectdb

121 if rel_to in known_models:
122 field_type = 'ForeignKey(%s' % rel_to
123 else:
124 field_type = "ForeignKey('%s'" % rel_to

#Modified inspectdb
121 if rel_to in known_models:
122 field_type = 'ForeignKey(%s' % rel_to
123 else:
124 field_type = "ForeignKey('%s'" % rel_to
125 extra_paramsrelated_name = '%s_%s' % (table2model(table_name),column_to_field_name[column_name])

to be added to:

https://github.com/django/django/blob/master/django/core/management/commands/inspectdb.py#L131

Change History (5)

comment:1 by Claude Paroz, 5 years ago

I'm not sure it is a change wanted by everyone. This can save some work in some configurations, and add a burden in other situations where the user don't need nor want to have related_name set.

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: duplicate
Status: newclosed
Summary: Automatic solution for “Add a related_name argument to the definition for 'XXX'.” for inspectdb.pyAutomatic solution for “Add a related_name argument to the definition for 'XXX'.” for inspectdb.py.
Type: UncategorizedNew feature
Version: 2.2master

Duplicate of #11179.

comment:3 by Mariusz Felisiak, 5 years ago

Summary: Automatic solution for “Add a related_name argument to the definition for 'XXX'.” for inspectdb.py.inspectdb should generate related_name for ForeignKey.

comment:4 by mingwugmail, 5 years ago

Resolution: duplicate
Status: closednew

Well, the problem is the models.py is generated by the utility python manage.py inspectdb, then when it's loaded e.g. python manage.py runserver, the system report hundreds of such errors.

If you think this feature may not be needed by everyone, at least add an option for people who *actually* need it; otherwise, these users have to do hundreds of modification on the *generated* file.

comment:5 by Mariusz Felisiak, 5 years ago

Resolution: duplicate
Status: newclosed

Please don't reopen tickets closed as duplicates. You can leave your comments in the original ticket but also don't reopen it. Follow triaging guidelines with regards to wontfix tickets.

Note: See TracTickets for help on using tickets.
Back to Top