justquick/django-activity-stream — pull request #206 by michaeljones (2014-10-09 11:11:51+00:00)
Title: Allow delayed registration with 'app.Model' strings

Hi,

These patches introduce support for delayed model registration to avoid issues with cross imports during django's model set up. The issue is that in django 1.6 the `registry.register` method is generally called directly from the users' `models.py` files during import but that method goes off and calls `get_model` which triggers another round of `model` imports which can then get tied up in knots.

The patch allows for delayed model registration using a `'myapp.MyModel'` strings which are processed from the `ActstreamConfig.ready` method which is generally called quite late in the app set up process (especially if `actstream` is placed last on the `INSTALLED_APPS` list.)

It allows the following approach:

```python
# in myapp/models.py
from actstream.registry import registry

class MyModel(models.Model):
...

registry.register('myapp.MyModel')
```

Rather than:

```python
from actstream.registry import registry

class MyModel(models.Model):
...

registry.register(MyModel)
```

Delaying the registration process like this makes the code more stable against circular imports and the issues that people have been experiencing. It isn't a perfect fix and I think in a lot of situations you can get around the import issues in other ways but this does make it more robust so that if people do use this approach they will be less likely to experience an import failure.

The annoying thing is that as I've tried to reproduce a case in which it was failing and this helped, the original case doesn't seem to fail any more. I've no idea what is going on. I'm going to share this as I've got halfway to sharing it a few times now only to be met by something like this. ie. it randomly working again.

Maybe it'll help someone who is otherwise resisting updating to 0.5.1.

Cheers,
Michael