开发者

Importing view functions to models.py - triggering via override save method

开发者 https://www.devze.com 2023-02-20 05:53 出处:网络
I would like to trigger a view function from within the save method of a model, in order to write or update an xml file associated with the instance.

I would like to trigger a view function from within the save method of a model, in order to write or update an xml file associated with the instance.

#models.py
from myapp.views import updateXML, createXML

class myModel(models.Model):

    def save(self, *args, **kwargs):
        if self.pk is not None:
            updateXML(self)
        else:
            createXML(s开发者_高级运维elf)
        super(FatherAgendaTemplate, self).save(*args, **kwargs)

#views.py
from myapp.models import otherModel

def createXML(instance):       
    print "create XML"
    print instance

def updateXML(instance):     
    print "update XML"
    print instance

The problem is that I need to import otherModel into the views.py which has a foreign key to myModel, and this causes a conflict of some sort and I get the error:

ImportError: cannot import name createXML

I suppose I'm going about this the wrong way, importing between models and views like this since it throws an import error. What's the correct way to do this? Of course I could do all of the xml writing functions from within models.py and avoid the import conflicts, but this seems like a messy approach.


Are the createXML and updateXML functions specific to that particular model? If so, as seems likely, then the best thing to do is to make them methods on myModel:

class MyModel(models.Model):
    ...fields...


    def createXML(self):
        ... do stuff with self ...

    def save(self, *args, **kwargs):
        if not self.pk:
            self.createXML()

and so on. For me, this is by far the best solution, and not messy at all.

However if this really doesn't work for you, there are a number of ways to avoid the import problem. Perhaps the best is to put the XML functions into a third module, called eg lib.py, which you can import into your models.

A third option is to do the import of the XML functions within the save method itself, rather than at the module level:

def save(self, *args, **kwargs):
    from myapp.views import createXML, updateXML

However I think the first two options would be preferable.


First of all, when posting a question of these sorts, its very important to post or link to all relative code (i.e. MyOtherModel) and a complete traceback.

Following the traceback leading to the error can help you find out why you are getting that ImportError, which might have nothing to do with you think might be your problem.

Second question: are those django views or ordinary functions? A view takes a request (with or without arguments) and should return a Response object.

In general, functions that operate or interact with an object (in this case in your model) should be functions inside your object, so called methods, and the views should call these methods.

class MyModel(models.Model):
    name = models.CharField...
    somethingelse = models.TextField...

    my_method_to_create_pdf(self):
        create_pdf(self.somethingelse)

if create_pdf is used not only here, create a lib in a third file and import here, but make any logic that has to do with models as methods, as objects should be self contained.

quick google link for object orientated programming in python: http://www.voidspace.org.uk/python/articles/OOP.shtml

cheers, ash

0

精彩评论

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