[Django]-Django – use of related_name '+'?

29👍

From Django docs, a tool to disallow backwards relation, their words:

If you’d prefer Django not to create a backwards relation, set related_name to ‘+’ or end it with ‘+’.

Above answers are correct, but I wanted to make the answer extra clear for others.

👤Marc

18👍

Perhaps when creating a reverse relationship would cause a conflict. Consider the case where you have an abstract model and two sub-classes of said model:

class MyAbstractModel(models.Model):

    class Meta(object):
        abstract = True

    book = models.ForeignKey(Books, related_name="+")

class MyThing(MyAbstractModel):
    name = models.CharField(max_length=128)

class MyOtherThing(MyAbstractModel):
    number = models.PositiveIntegerField()

Without the use of "+", you’d get a naming conflict and Django would refuse to start. Given that you don’t actually need it in this case, it makes sense to just skip it.

Leave a comment