这是我的模型
class Userlist(models.Model):
username = models.CharField(max_length=20)
class Mailinglist(models.Model):
users = models.ManyToManyField(Userlist, blank=True)
def __init__(self, *args, **kwargs):
super(Mailinglist, self).__init__(*args, **kwargs)
userlist = Userlist.objects.all().filter(mailinglist__id=self.pk)
setattr(self, '__original_userlist', userlist)
def log(self):
userlist = Userlist.objects.filter(mailinglist__id=self.pk)
original = getattr(self, '__original_userlist')
print(userlist) #HERE1
print(original) #HERE2
def save(self, force_insert=False, force_update=False, *args, **kwargs):
super(Mailinglist, self).save(force_insert, force_update, *args, **kwargs)
self.log()
我正在尝试在我的邮件列表中添加或删除用户之前和之后的值,但是#whore 1 和中的值是相同的(新值)#there2 。有什么想法我如何获得旧价值?
好,我修复了。Django一旦真正需要查询就会调用查询。(懒惰查询?)
我所做的是强制查询。
我以set
的方式列出了列表def __init__(self, *args, **kwargs):
super(Mailinglist, self).__init__(*args, **kwargs)
userlist = Userlist.objects.all().filter(mailinglist__id=self.pk)
setattr(self, '__original_userlist', set(userlist))
然后
def log(self):
userlist = Userlist.objects.filter(mailinglist__id=self.pk)
original = getattr(self, '__original_userlist')
print(set(userlist)) #HERE1
print(original) #HERE2
这样,我什至可以与
进行比较diff = set_original - set_actual