Opened 16 years ago

Closed 16 years ago

Last modified 16 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: dev
Severity: Keywords: abstract model inheritance
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 by Brian Rosner, 16 years ago

Resolution: invalid
Status: newclosed
Version: newforms-adminSVN
Note: See TracTickets for help on using tickets.
Back to Top