Django 1.9 deprecation warnings app_label

后端 未结 12 709
花落未央
花落未央 2020-12-01 05:09

I\'ve just updated to Django v1.8, and testing my local setup before updating my project and I\'ve had a deprecation warning that I\'ve never seen before, nor does it make a

12条回答
  •  北海茫月
    2020-12-01 05:24

    The easiest and simplest way to solve this is to place the "import signals" command at the BOTTOM of the Model file you are working with. This ensures that the models are all loaded before the signals for that model are imported. You would have to do this for each model you are importing (if you use receivers linked to particular models), or in the models.py for the app that comes at the end of the "installed apps" in your settings.

    The other solutions are only required if you are dealing with non-model type signals, where the models will never be imported first.

    Why this is not noted in the docs is a mystery, because I just got caught by it, until I remembered that you have to do that.

    models.py

    from django.db import models
    
    class MyModel(models.Model):
        myfield1 = models.CharField()
        myfield2 = models.CharField()
    
    import signals
    

    And then in signals.py:

    from django.db.models.signals import pre_save # Or whatever you are using
    from django.dispatch import receiver
    
    from .models import MyModel
    
    @receiver(pre_save, sender=MyModel)
    def my_receiver(sender, instance, **kwargs):
        mysender = sender
        print mysender
    

    Like that.

提交回复
热议问题