Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to resolve circular import involving haystack?

Haystack

haystack_signal_processor let you use custom signal processor to initiate index for certain models.

I have in my settings.py

HAYSTACK_SIGNAL_PROCESSOR='my_app.signals.MySignalProcessor'
(this imports signals.py so. this is settings -> signals)

then inside my signals.py I have
from my_app.models import my_model # to connect my_model
And my_app.models.py has from django.conf import settings
(signals -> models -> settings)

How do I resolve this circular import?

like image 656
eugene Avatar asked Jul 08 '13 07:07

eugene


1 Answers

taken from https://github.com/PitonFoundation/atlas/commit/cc0abcb

Instead of importing the model on top of your signals.py file, import the models in the methods of your custom SignalProcessor using get_model:

from django.db.models.loading import get_model

class MySignalProcessor(signals.BaseSignalProcessor):
    def setup(self):
        MyModel = get_model('myApp', 'MyModel')
        models.signals.post_save.connect(self.handle_save, sender=MyModel)
like image 183
Michael Avatar answered Oct 17 '22 03:10

Michael