开发者

Django: Making ForeignKey not create a back-reference

开发者 https://www.devze.com 2022-12-19 22:01 出处:网络
I know that I can use ForeignKey\'s related_name argument to control what the back-reference\'s name will be. But is it possible to avoid creating a back-reference completely?

I know that I can use ForeignKey's related_name argument to control what the back-reference's name will be. But is it possible to avoid creating a back-reference completely?

(e.g., I have in Car a field ForeignKey(Pers开发者_开发百科on), and I don't want Person to have an attribute that leads backs to Car.)


For those coming from Google: use a + in the related_name field to prevent creating a back reference field.

https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_name


Why would you want to do this? You don't have to use it if you don't want to.

In any case, the back-reference is only a code shortcut - it's exactly equivalent to Car.objects.filter(person_id=person.id).

0

精彩评论

暂无评论...
验证码 换一张
取 消