[ACCEPTED]-Django: Why do some model fields clash with each other?-django-models

Accepted answer
Score: 299

You have two foreign keys to User. Django 7 automatically creates a reverse relation 6 from User back to GameClaim, which is usually 5 gameclaim_set. However, because you have two FKs, you 4 would have two gameclaim_set attributes, which is obviously 3 impossible. So you need to tell Django what 2 name to use for the reverse relation.

Use 1 the related_name attribute in the FK definition. e.g.

class GameClaim(models.Model):
    target = models.ForeignKey(User, related_name='gameclaim_targets')
    claimer = models.ForeignKey(User, related_name='gameclaim_users')
    isAccepted = models.BooleanField()
Score: 8

The User model is trying to create two fields 6 with the same name, one for the GameClaims that have 5 that User as the target, and another for the GameClaims that 4 have that User as the claimer. Here's the docs on related_name, which is 3 Django's way of letting you set the names 2 of the attributes so the autogenerated ones 1 don't conflict.

Score: 7

The OP isn't using a abstract base class... but 16 if you are, you will find that hard coding 15 the related_name in the FK (e.g. ..., related_name="myname") will 14 result in a number of these conflict errors 13 - one for each inherited class from the 12 base class. The link provided below contains 11 the workaround, which is simple, but definitely 10 not obvious.

From the django docs...

If you 9 are using the related_name attribute on 8 a ForeignKey or ManyToManyField, you must 7 always specify a unique reverse name for 6 the field. This would normally cause a problem 5 in abstract base classes, since the fields 4 on this class are included into each of 3 the child classes, with exactly the same 2 values for the attributes (including related_name) each 1 time.

More info here.

Score: 2

Sometimes you have to use extra formatting 6 in related_name - actually, any time when inheritance 5 is used.

class Value(models.Model):
    value = models.DecimalField(decimal_places=2, max_digits=5)
    animal = models.ForeignKey(
        Animal, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class Height(Value):
    pass

class Weigth(Value):
    pass

class Length(Value):
    pass

No clash here, but related_name 4 is defined once and Django will take care 3 for creating unique relation names.

then 2 in children of Value class, you'll have 1 access to:

herdboard_height_related
herdboard_lenght_related
herdboard_weight_related
Score: 0

I seem to come across this occasionally 14 when I add a submodule as an application 13 to a django project, for example given the 12 following structure:

myapp/
myapp/module/
myapp/module/models.py

If I add the following 11 to INSTALLED_APPS:

'myapp',
'myapp.module',

Django seems to process 10 the myapp.mymodule models.py file twice 9 and throws the above error. This can be 8 resolved by not including the main module 7 in the INSTALLED_APPS list:

'myapp.module',

Including the 6 myapp instead of myapp.module causes all the database tables 5 to be created with incorrect names, so this 4 seems to be the correct way to do it.

I came 3 across this post while looking for a solution 2 to this problem so figured I'd put this 1 here :)

Score: 0

Just adding to Jordan's answer (thanks for 9 the tip Jordan) it can also happen if you 8 import the level above the apps and then 7 import the apps e.g.

myproject/ apps/ foo_app/ bar_app/

So if you are importing 6 apps, foo_app and bar_app then you could 5 get this issue. I had apps, foo_app and 4 bar_app all listed in settings.INSTALLED_APPS

And 3 you want to avoid importing apps anyway, because 2 then you have the same app installed in 1 2 different namespaces

apps.foo_app and foo_app

More Related questions