related_name parameter name conflict in django?

Question:

i have a query regarding "related_name" parameter (used while establishing a relationship between models)

suppose i assign the value to related_name= "x"

then,

  1. it sets the given value to reverse relation manager name
    ex- parent_model_object.x.all()

  2. it sets given name to parent model while filtering objects.
    ex – child_model.objects.filter(x__name = "manav")

so is there any chances of name conflict ? (as same name is assigned to model_class and reverse relation manager )

Asked By: harshit

||

Answers:

so is there any chances of name conflict?

No: Django checks this. It will try to add the name in reverse, and it will print an error in case there is.

The .filter(…) [Django-doc] by the way does not work with the related_name=… [Django-doc], but with the related_query_name=… [Django-doc]. If no related_query_name=… is set, it will default to the related_name=…, and if that is missing too, to the model name in lowercase. This is different from related_name=… itself, which defaults to the modelname_set with modelname the name of the model in lowercase.