相关疑难解决方法(0)

Django:保存时,如何检查字段是否已更改?

在我的模型中,我有:

class Alias(MyBaseModel):
    remote_image = models.URLField(max_length=500, null=True, help_text="A URL that is downloaded and cached for the image. Only
 used when the alias is made")
    image = models.ImageField(upload_to='alias', default='alias-default.png', help_text="An image representing the alias")


    def save(self, *args, **kw):
        if (not self.image or self.image.name == 'alias-default.png') and self.remote_image :
            try :
                data = utils.fetch(self.remote_image)
                image = StringIO.StringIO(data)
                image = Image.open(image)
                buf = StringIO.StringIO()
                image.save(buf, format='PNG')
                self.image.save(hashlib.md5(self.string_id).hexdigest() + ".png", ContentFile(buf.getvalue()))
            except IOError :
                pass
Run Code Online (Sandbox Code Playgroud)

这首次remote_image变化很有效.

当有人修改remote_image了别名时,如何获取新图像?其次,是否有更好的方法来缓存远程图像?

django caching image django-models

273
推荐指数
13
解决办法
14万
查看次数

django-simple-history 中 ManyToManyField 的历史

简而言之,我需要在历史记录中保存对我的模型之一的多对多字段所做的更改。

我可以从https://github.com/Kyruus/django-simple-history/commit/5ba8d2b4d72819f154a11f297796e6a2bb7172bf看到 支持M2M。然而,每当我对 M2M 领域进行更改时,它在所有历史记录中也会发生变化,就好像从未更改过一样。我是 django 和 python 的新手,所以也许我错过了一些东西。

我的模型.py:

from django.db import models
from simple_history.models import HistoricalRecords

class Student(models.Model):
  studentname = models.CharField(max_length=50, verbose_name='Name')

class ClassRoom(models.Model):
  classname = models.CharField(max_length=50, verbose_name='Name')
  students = models.ManyToManyField(Student)
  history = HistoricalRecords()
Run Code Online (Sandbox Code Playgroud)

我的管理员.py:

from django.contrib import admin
from school.models import Student, ClassRoom
from simple_history.admin import SimpleHistoryAdmin

class StudentAdmin(SimpleHistoryAdmin):
  list_display = ('studentname',)

class ClassRoomAdmin(SimpleHistoryAdmin):
  list_display = ('classname',)

admin.site.register(Student,StudentAdmin)
admin.site.register(ClassRoom, ClassRoomAdmin)
Run Code Online (Sandbox Code Playgroud)

我通过以下方式安装了 django-simple-history:

>pip install django-simple-history
Run Code Online (Sandbox Code Playgroud)

python django django-models django-admin

5
推荐指数
1
解决办法
2721
查看次数

如何使用 django-simple-history 存储 ManyToManyField 的历史记录。

如何使用 django-simple-history 存储 ManyToManyField 的历史记录。我使用带有属性 m2m_filds 的 HistoricalRecords 但它抛出错误:意外的关键字参数 'm2m_fields'

python django history manytomanyfield django-simple-history

4
推荐指数
1
解决办法
4834
查看次数