59

I want to have two foreign keys to the same model:

class Test(models.model):
    example1 = models.ForeignKey(Example)
    example2 = models.ForeignKey(Example)

I get errors like:

Accessor for field 'example1' clashes with related field 'Example.test_set'. Add a related_name argument to the definition for 'example1'.

viam0Zah
  • 25,949
  • 8
  • 77
  • 100
MikeN
  • 45,039
  • 49
  • 151
  • 227

4 Answers4

130

Try using related_name:

class Test(models.model):
    example1 = models.ForeignKey('Example', related_name='example1')
    example2 = models.ForeignKey('Example', related_name='example2')
strager
  • 88,763
  • 26
  • 134
  • 176
  • 2
    I have two models in app that tries to do this. The second one works fine with this method: it has to foreign keys to the first model. The first model does not work. Maybe it is because it is referring to an imported model instead. Has anyone made this work with imported models? – SpiRail Jan 31 '13 at 00:26
  • 2
    Are those single quotations are necessary for `Example `model? – Ghasem Nov 20 '18 at 04:21
  • Quite new in Django, I wonder why a FK should be shown twice in a model. Given that you can access it once, you can get all its attributes later, can't you ? What example of use ? – Abpostman1 Mar 12 '22 at 14:18
  • 1
    @Abpostman1 because maybe it makes sense in your model to have both. For example you have some sort of model that has a source and a destination that are both of the same type. – Adrian Pop Mar 28 '22 at 15:51
  • @Adrian. Thanks for explanation. This way I understand the merits of doing this way – Abpostman1 Mar 29 '22 at 07:17
29

Django uses some python magic to define relationships between models, some of which involves using the name of the models in the relationships (that's where the 'test' in 'test__set' is coming from.) What's happening, I would guess, is that it's trying to put "test__set" in the Example model twice, once for each foreign key you've got defined.

The error message suggests something to try: define a related_name argument (overriding one of those 'test_set's) that it can use instead of auto-generating two clashing names.

More info here: page has been removed

Current page relating to model relationships: https://docs.djangoproject.com/en/2.0/ref/models/fields/#module-django.db.models.fields.related

markwalker_
  • 12,078
  • 7
  • 62
  • 99
Matt
  • 10,434
  • 1
  • 36
  • 45
  • also see [Following relationships “backward”](https://docs.djangoproject.com/en/2.2/topics/db/queries/#following-relationships-backward) – Tjorriemorrie May 01 '19 at 00:46
8

Just do what the error message tells you to do, and if you're unsure what that means, consult the documentation for related_name.

James Bennett
  • 10,903
  • 4
  • 35
  • 24
4

In django 2.0 Try this:

user = models.ForeignKey(User, on_delete=models.PROTECT, null=True,  related_name='user')
paper = models.ForeignKey(paperRecord, on_delete=models.PROTECT, null=True,  related_name='paper')
pranjal0819
  • 270
  • 2
  • 17