I’m plugging Django into an existing system. I have been given a legacy schema, and I have to work with it.
I have generated models with inspectdb, but I’m getting hundreds of:
ABC>DEF: Accessor for field 'HIJ' clashes with related field 'KLM'. Add a related_name argument to the definition for 'HIJ'.
I wouldn’t have a problem with this if it were only a small number. But it isn’t.
Is there any automatic way to fix this by adding unique related_names?
EDIT: For clarification, I do am looking for an automatic solution to the problem, not one that involves editing each field by hand.
If you don’t need the related name, you can set it to “+”.
If you don’t want to disable them completely or cannot use “+” in your django version (I can’t remember when it was added), you may use something like the following:
But if you insert related names with a regexp search and replace, you may as well set them to something readable, e.g. replace
(\s*(\w+)\s*=\s*models\.ForeignKey\(.*)\)(\s*)$with$1, related_name="%(class)s_$2_reverse")$3