Django 错误<模型>对象没有属性“更新" [英] Django error <model> object has no attribute 'update'

查看:34
本文介绍了Django 错误<模型>对象没有属性“更新"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

我正在对服务器进行一些维护并重新启动......一旦它回来,代码就可以正常工作......这实际上让我同样担心......

I was doing some maintenance on the server and rebooted... once it came back the code worked just fine... which actually makes me to worry just the same...

我认为这是 mod_wsgi 的一个错误.

i think it's a bug on mod_wsgi.

还是谢谢!

我真的是 django 的新手(昨天开始).我设法使用 xlrd 制作了一个 excel 解析器,数据一切正常(它加载非常快),我需要更新数据库中的文件信息,这样我才能知道负载是如何进行的,这就是我拥有的地方问题,save() 方法不起作用,我已经使用了 update 以及 get 和 filter,但总是同样的问题.

I'm really new to django (started yesterday). I managed to make a excel parser using xlrd, everything works fine with the data (it loads really really fast), i need to update the file info in the database so i can know how the load is going, this is where i have the problem, the save() method doesn't work, I already used update along with get and filter, but always the same problem.

希望大家能指出错误在哪里

I hope you can point me out where is the mistake

models.py

class archivo(models.Model):
    archivo_id = models.AutoField(primary_key=True)
    fk_cliente = models.IntegerField()
    fk_usuario = models.IntegerField()
    archivo_nombre = models.CharField(max_length = 30)
    archivo_original = models.CharField(max_length = 255)
    archivo_extension = models.CharField(max_length = 5)
    archivo_tamano = models.FloatField()
    archivo_registros = models.IntegerField()
    archivo_registros_buenos = models.IntegerField()
    archivo_registros_malos = models.IntegerField()
    archivo_registros_cargados = models.IntegerField()
    archivo_fecha_carga = models.DateTimeField()
    archivo_fecha_envio = models.DateTimeField()
    def __unicode__(self):
        return self.archivo_id

views.py

from procesa.models import *
from django.conf import settings
from django.shortcuts import render_to_response  
import xlrd
from time import strftime
from symbol import except_clause
def procesa(request, procesar = 0):
    datos = None
    infoarchivo = None
    if(procesar > 0):
        try:
            infoarchivo = archivo.objects.get(archivo_id=int(procesar))
        except:
            return render_to_response('error.html')

    if (infoarchivo is not None):
        excel_path = settings.FILES_URL+infoarchivo.archivo_original
        wb = xlrd.open_workbook(str(excel_path))
        sh = wb.sheet_by_index(0)
        ##START UPDATE##
        infoarchivo2 = archivo.objects.filter(archivo_id = procesar)
        infoarchivo2.archivo_registros = sh.nrows
        infoarchivo2.save()
        ##END UPDATE##            
        for rownum in range(sh.nrows):
            destino = str(sh.cell(rownum,0).value)
            destino = destino.replace(".0","")
            if (int(destino) > 0):
                mensaje = str(sh.cell(rownum,1).value)
                ahora = strftime("%Y-%m-%d %H:%M:%S")
                reg = registro.objects.filter(registro_destino__exact=destino,fk_archivo__exact=procesar)
                #reg = registro.objects.raw(str(el_query))

                if (reg.exists()):
                    exists = True
                else:
                    r = registro(fk_cliente=1,fk_usuario=1,fk_archivo=int(procesar),registro_destino=destino,registro_mensaje=mensaje,registro_estado='Cargado',registro_fecha_carga=ahora)
                    r.save()


        datos = {'ID':procesar,'PATH': settings.FILES_URL, 'INFO':infoarchivo, 'el_excel':infoarchivo.archivo_original, 'registros':sh.nrows }
        return render_to_response('carga.html', {'datos': datos})

在我已经尝试过的##START UPDATE## 块中

in the ##START UPDATE## block i've already tried with

infoarchivo.archivo_registros = sh.nrows
infoarchivo.save()

archivo.objects.filter(archivo_id = procesar).update(archivo_registros=sh.nrows)

archivo.objects.get(archivo_id = procesar).update(archivo_registros=sh.nrows)

我找不到对此错误的任何引用或其他要添加到模型文件中的内容,我很确定这很容易修复,但我就是找不到.

I can't find any reference to this error or something else to add in the models file, i'm pretty sure it's something really easy to fix, but i just can't find it.

我得到的错误(对于所有不同的代码)是

The error i'm getting (for all the different codes) is

异常类型:AttributeError at/procesa/4

异常值:'archivo' 对象没有属性'update'

文件的记录被解析并插入没有问题.

The records of the file gets parsed and inserted with no problem.

我在 Apache 2.2 中使用 Django 1.5 和 python 2.7,并在 Amazon 上的 EC2 中安装了 mod_wsgi 和 mysql 后端

I'm using Django 1.5 with python 2.7 in Apache 2.2 with mod_wsgi and mysql backend installed in EC2 on Amazon

更新我正在对服务器进行一些维护并重新启动......一旦它回来,代码就可以正常工作......这实际上让我担心同样......

UPDATE I was doing some maintenance on the server and rebooted... once it came back the code worked just fine... which actually makes me to worry just the same...

我认为这是 mod_wsgi 的一个错误.

i think it's a bug on mod_wsgi.

还是谢谢!

推荐答案

这个错误的原因是.get()返回一个单独的对象,.update() 仅适用于查询集,例如使用 .filter() 而不是 .get() 返回的内容.

The reason for this error is that .get() returns an individual object and .update() only works on querysets, such as what would be returned with .filter() instead of .get().

如果您使用的是 .get(),那么 .update() 将不起作用.您需要手动将信息保存到对象中:

If you are using .get(), then .update() will not work. You will need to save the information to the object manually:

archivo = archivo.objects.get(archivo_id=procesar)
archivo.archivo_registros = sh.nrows
archivo.save()

您也可以使用 update_fields 如果您只想保存此特定数据:

You can also use update_fields if you only wish to save this particular piece of data:

archivo = archivo.objects.get(archivo_id=procesar)
archivo.archivo_registros = sh.nrows
archivo.save(update_fields=['archivo_registros'])

这可以防止触发您可能不想调用的任何信号.

This prevents triggering any signals that you may not want to invoke.

您的另一个选择是简单地使用 .filter().

Your other option is to simply use .filter().

archivo = archivo.objects.filter(archivo_id=procesar).update(archivo_registros=sh.nrows)

请注意,如果多个对象存在,这将更新它们.如果您想确保不会发生这种情况,您应该在过滤器中包含主键,或者使用较早的方法之一来确保您只修改单个对象.

Note that this will update multiple objects if they exist. If you want to be sure that doesn't happen, you should include the primary key in the filter, or use one of the earlier approaches to make sure you are only modifying a single object.

这篇关于Django 错误<模型>对象没有属性“更新"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆