Kaydet (Commit) ff445f4c authored tarafından Kevin Christopher Henry's avatar Kevin Christopher Henry Kaydeden (comit) Tim Graham

Fixed #26616 -- Clarified model usage in AppConfig.ready().

üst 4773ed2e
...@@ -239,13 +239,27 @@ Methods ...@@ -239,13 +239,27 @@ Methods
as registering signals. It is called as soon as the registry is fully as registering signals. It is called as soon as the registry is fully
populated. populated.
You cannot import models in modules that define application configuration Although you can't import models at the module-level where
classes, but you can use :meth:`get_model` to access a model class by :class:`~django.apps.AppConfig` classes are defined, you can import them in
name, like this:: ``ready()``, using either an ``import`` statement or
:meth:`~AppConfig.get_model`.
If you're registering :mod:`model signals <django.db.models.signals>`, you
can refer to the sender by its string label instead of using the model
class itself.
Example::
from django.db.models.signals import pre_save
def ready(self): def ready(self):
# importing model classes
from .models import MyModel # or...
MyModel = self.get_model('MyModel') MyModel = self.get_model('MyModel')
# registering signals with the model's string label
pre_save.connect(receiver, sender='app_label.MyModel')
.. warning:: .. warning::
Although you can access model classes as described above, avoid Although you can access model classes as described above, avoid
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment