Skip to content

Instantly share code, notes, and snippets.

@imgVOID
Created July 2, 2021 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imgVOID/9e010faf058b0de8ce539dab6efe018f to your computer and use it in GitHub Desktop.
Save imgVOID/9e010faf058b0de8ce539dab6efe018f to your computer and use it in GitHub Desktop.
Поле отношения ForeignKey
from django.db import models
class ModelA(models.Model):
name = models.CharField()
class ModelB(models.Model):
a = models.ForeignKey(ModelA)
# "Прямое" поле отношения ForeignKey:
ModelB.objects.select_related('a').all()
# "Обратное" поле отношения ForeignKey:
ModelA.objects.prefetch_related('modelb_set').all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment