将视图函数导入到 models.py - 通过覆盖 save 方法触发

发布于 2024-10-25 19:33:40 字数 817 浏览 4 评论 0原文

我想从模型的 save 方法中触发视图函数,以便写入或更新与实例关联的 xml 文件。

#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(self)
        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

问题是我需要将 otherModel 导入到有 myModel 外键的views.py 中,这会导致某种冲突,并且出现错误:

ImportError: cannot import name createXML

我想我会以错误的方式处理这个问题,在之间导入像这样的模型和视图,因为它会引发导入错误。这样做的正确方法是什么?当然,我可以在 models.py 中完成所有 xml 编写功能并避免导入冲突,但这似乎是一种混乱的方法。

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(self)
        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被你宠の有点坏 2024-11-01 19:33:40

createXMLupdateXML 函数是否特定于该特定模型?如果是这样,那么最好的办法就是让它们成为 myModel 上的方法:

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


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

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

等等。对我来说,这是迄今为止最好的解决方案,而且一点也不混乱。

但是,如果这确实不适合您,有多种方法可以避免导入问题。也许最好的方法是将 XML 函数放入第三个模块中,例如 lib.py,您可以将其导入到模型中。

第三个选项是在 save 方法本身内导入 XML 函数,而不是在模块级别:

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

但是我认为前两个选项更好。

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.

逆光下的微笑 2024-11-01 19:33:40

首先,在发布此类问题时,发布或链接到所有相关代码(即 MyOtherModel)和完整的回溯非常重要。

跟踪导致错误的回溯可以帮助您找出为什么会出现 ImportError,这可能与您认为可能是您的问题无关。

第二个问题:那些是 django 视图还是普通函数?视图接受请求(带或不带参数)并应返回 Response 对象。

一般来说,操作对象或与对象交互的函数(在本例中是在模型中)应该是对象内部的函数,即所谓的方法,并且视图应该调用这些方法。

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

    my_method_to_create_pdf(self):
        create_pdf(self.somethingelse)

如果create_pdf不仅在这里使用,
在第三个文件中创建一个库并在此处导入,但将与模型有关的任何逻辑作为方法,因为对象应该是自包含的。

python中面向对象编程的快速谷歌链接:http://www.voidspace.org .uk/python/articles/OOP.shtml

干杯,

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文