Django模型保存问题

时间:2013-03-21 11:02:51

标签: python django model save

我会在这里清楚我的情况。 我有一些模型:

class Messages(models.Model):
    message_idnr = models.BigIntegerField(primary_key=True)
    mailbox_idnr = models.ForeignKey(Mailboxes, null=True, db_column='mailbox_idnr', blank=True)
    physmessage = models.ForeignKey(Physmessage, null=True, blank=True)

    def __unicode__(self):
        return '%s'%(self.physmessage)
    class Meta:
        db_table = u'messages'


class Mailboxes(models.Model):
    mailbox_idnr = models.BigIntegerField(primary_key=True)
    owner_idnr = models.ForeignKey(Users, null=True, db_column='owner_idnr', blank=True)
    name = models.CharField(max_length=255)
    permission = models.SmallIntegerField(default =2)
    seq = models.BigIntegerField(default = 0)
    class Meta:
        db_table = u'mailboxes'


class Users(models.Model):
    user_idnr = models.BigIntegerField(primary_key=True)
    userid = models.CharField(max_length=100, unique=True)
    passwd = models.CharField(max_length=130)
    last_login = models.DateTimeField()
    def __unicode__(self):
        return '%s'%(self.userid)

    class Meta:
        db_table = u'users'

class Physmessage(models.Model):
    id = models.BigIntegerField(primary_key=True)
    messagesize = models.BigIntegerField()
    rfcsize = models.BigIntegerField()
    internal_date = models.DateTimeField(null=True, blank=True)
    def __unicode__(self):
        return '%s'%(self.id)
    class Meta:
        db_table = u'physmessage'

然后,我有一个views.py调用此函数,如

def dbmail_move_email_to_dir(mailbox,maildir,mailvalue,destination):
    db_user = Users.objects.get(userid = mailbox.email)
    if maildir.lower() == 'inbox':
        db_source_dir = Mailboxes.objects.get(Q(owner_idnr = db_user),Q(name = 'INBOX'))
    else:
        db_source_dir = Mailboxes.objects.get(Q(owner_idnr = db_user),Q(name = 'INBOX.%s'%maildir))
    db_des_dir = Mailboxes.objects.get(Q(owner_idnr = db_user),Q(name = 'INBOX.%s'%destination))
    for value in mailvalue:
        print 'physical value of email %s'%value
        print 'from maildir id %s'%db_source_dir.mailbox_idnr
        print 'to maildir id %s'%db_des_dir.mailbox_idnr
        print Physmessage.objects.get(id = value)
        print len(Messages.objects.filter(physmessage = Physmessage.objects.get(id = value)))
        for row in Messages.objects.filter(physmessage = Physmessage.objects.get(id = value)):
            print 'delete each row %s'%row
            print 'current mailbox %s'%row.mailbox_idnr.mailbox_idnr
            row.mailbox_idnr=db_des_dir
            print 'current mailbox %s'%row.mailbox_idnr.mailbox_idnr
            row.save()
            print 'current mailbox %s'%row.mailbox_idnr.mailbox_idnr

    return True

当我从测试文件运行此函数时,我得到的结果如

physical value of email 15
from maildir id 128
to maildir id 126
15
1
delete each row 15
current mailbox 128
current mailbox 126
current mailbox 126

它会将mailbox_idnr更改为新值,我在postgres数据库中看到它。但是当我在视图中调用此函数时,登录打印是相同的,但mailbox_idnr不会更改。

physical value of email 46
from maildir id 125
to maildir id 128
46
1
delete each row 46
current mailbox 125
current mailbox 128
current mailbox 128

希望每个人都可以建议修复它。我有登录管理员,也可以更改它。但在我看来,我无法改变它。不能删除它。

谢谢,

0 个答案:

没有答案