Skip to main content
 首页 » 编程设计

python之在 Django 中向我的模型添加历史 View

2025年05月04日20傻小

当我在 Django 模型中进行更改时,如何在管理中添加历史记录?当我保存在其他模板或 shell 中时

我正在使用 Django 1.6 在管理员中。更改已存储,但在另一个模板中我看不到这些更改。我该如何解决这个问题?

class Modelo(models.Model): 
    nombre = models.CharField(max_length=100) 
    marca = models.ForeignKey(Marca) 
    def __unicode__(self): 
        return self.nombre 
    class Meta: 
        unique_together = ("nombre", "marca") 
        verbose_name_plural = u'Modelos' 
        verbose_name=u'Modelos' 

请您参考如下方法:

您可以使用 django-simple-history应用

一些简单的用法(来自文档,在您配置应用程序之后):

>>> from polls.models import Poll, Choice 
>>> from datetime import datetime 
>>> poll = Poll.objects.create(question="what's up?", pub_date=datetime.now()) 
>>> 
>>> poll.history.all() 
[<HistoricalPoll: Poll object as of 2010-10-25 18:03:29.855689>]