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?
(eg, I have in Car a field ForeignKey(Person), 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)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.