开发者

Django: How to dynamically add tag field to third party apps without touching app's source code

开发者 https://www.devze.com 2022-12-23 09:03 出处:网络
Scenario: large project with many third party apps. Want to add tagging to those apps without having to modify the apps\' source.

Scenario: large project with many third party apps. Want to add tagging to those apps without having to modify the apps' source.

My first thought was to first specify a list of models in settings.py (like ['appname.modelname',], and call django-tagging's register function on each of them. The register function adds a TagField and a custom manager to the specified model. The problem with 开发者_如何转开发that approach is that the function needs to run BEFORE the DB schema is generated.

I tried running the register function directly in settings.py, but I need django.db.models.get_model to get the actual model reference from only a string, and I can't seem to import that from settings.py - no matter what I try I get an ImportError. The tagging.register function imports OK however.

So I changed tactics and wrote a custom management command in an otherwise empty app. The problem there is that the only signal which hooks into syncdb is post_syncdb which is useless to me since it fires after the DB schema has been generated.

The only other approach I can think of at the moment is to generate and run a 'south' like database schema migration. This seems more like a hack than a solution.

This seems like it should be a pretty common need, but I haven't been able to find a clean solution.

So my question is: Is it possible to dynamically add fields to a model BEFORE the schema is generated, but more specifically, is it possible to add tagging to a third party model without editing it's source.

To clarify, I know it is possible to create and store Tags without having a TagField on the model, but there is a major flaw in that approach in that it is difficult to simultaneously create and tag a new model.


From the docs:

You don't have to register your models in order to use them with the tagging application - many of the features added by registration are just convenience wrappers around the tagging API provided by the Tag and TaggedItem models and their managers, as documented further below.

Take a look at the API documentation and the examples that follow for how you can add tags to any arbitrary object in the system.

http://api.rst2a.com/1.0/rst2/html?uri=http://django-tagging.googlecode.com/svn/trunk/docs/overview.txt#tags

Updated

#views.py
def tag_model_view(request, model_id):
    instance_to_tag = SomeModel.objects.get(pk=model_id)
    setattr(instance_to_tag, 'tags_for_instance', request.POST['tags'])
    ...
    instance_to_tag.save()
    ...returns response

#models.py
#this is the post_save signal receiver
def tagging_post_save_handler(sender, instance, created):
    if hasattr(instance, 'tags_for_instance'):
        Tag.objects.update_tags(instance, instance.tags_for_instance)
0

精彩评论

暂无评论...
验证码 换一张
取 消