Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#7545 closed (invalid)

Can't create an abstract model with 2 foreign keys to the same model

Reported by: djangoproject.com@… Owned by: nobody
Component: Uncategorized Version: master
Severity: Keywords: abstract model inheritance
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I am using newforms-admin revision 7770.

If you create an abstract model with 2 foreign keys (or generic relationships) to the same model and then inherit from the abstract model more than once you end up with "clashes with related field" errors.

Here is an example models.py:

from django.db import models

class Foo(models.Model):
	title = models.CharField(max_length=200)

class MyAbstraction(models.Model):
	"""
	If 'related_name' is removed from 'foo' and 'bar' is removed altogether, then everything works as expected.
	"""
	foo = models.ForeignKey('Foo', related_name='plop')
	bar = models.ForeignKey('Foo')

	class Meta:
		abstract = True

class Extended(MyAbstraction):
	bax = models.CharField(max_length=200)

class AnotherExtended(MyAbstraction):
	baz = models.CharField(max_length=200)

And it'll spit out these errors:

Error: One or more models did not validate:
myproject.extended: Accessor for field 'foo' clashes with related field 'Foo.plop'. Add a related_name argument to the definition for 'foo'.
myproject.extended: Reverse query name for field 'foo' clashes with related field 'Foo.plop'. Add a related_name argument to the definition for 'foo'.
myproject.anotherextended: Accessor for field 'foo' clashes with related field 'Foo.plop'. Add a related_name argument to the definition for 'foo'.
myproject.anotherextended: Reverse query name for field 'foo' clashes with related field 'Foo.plop'. Add a related_name argument to the definition for 'foo'.

Change History (2)

comment:1 Changed 7 years ago by brosner

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed
  • Version changed from newforms-admin to SVN
Note: See TracTickets for help on using tickets.
Back to Top