From f927c9f2aac2d41ddbb7454da6470a9e93b26c38 Mon Sep 17 00:00:00 2001 From: Clifford Gama Date: Mon, 13 Jan 2025 21:15:57 +0200 Subject: [PATCH] [5.1.x] Fixed #36095 -- Introduced lazy references in "Models across files" section. Backport of 6a2c296e706a0b8f9f9b89e66b37001ce2a03ea7 from main. --- docs/topics/db/models.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt index b543fd6759..5e7fe4b52e 100644 --- a/docs/topics/db/models.txt +++ b/docs/topics/db/models.txt @@ -724,6 +724,24 @@ refer to the other model class wherever needed. For example:: null=True, ) +Alternatively, you can use a lazy reference to the related model, specified as +a string in the format ``"app_label.ModelName"``. This does not require the +related model to be imported. For example:: + + from django.db import models + + + class Restaurant(models.Model): + # ... + zip_code = models.ForeignKey( + "geography.ZipCode", + on_delete=models.SET_NULL, + blank=True, + null=True, + ) + +See :ref:`lazy relationships ` for more details. + Field name restrictions -----------------------