Django的错误&LT;模型&GT;对象有没有属性'更新' [英] Django error <model> object has no attribute 'update'
问题描述
更新
我是做服务器上的一些维护并重新启动......一旦它回来了code工作就好了......这实际上使我只是担心一样的...
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的一个bug。
i think it's a bug on mod_wsgi.
不管怎样,谢谢!
我真的很新的Django(昨天开始)。我设法用xlrd做出一个excel解析器,一切正常的数据(它加载真的真的快),我需要更新数据库中的文件信息,这样我就可以知道负载是怎么回事,这是我有问题,save()方法是不行的,我已经使用更新get和滤镜一起,但总是同样的问题。
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})
在我已经有试过##开始更新##块
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)
我找不到任何引用此错误或别的东西在模型文件中添加,我是pretty确保它的东西真的很容易解决,但我只是无法找到它。
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.
我得到(所有不同codeS)的误差
The error i'm getting (for all the different codes) is
异常类型:AttributeError的AT / PROCESA / 4
例外值:'archivo'对象有没有属性'更新'
的文件的记录被解析并没有问题插入
The records of the file gets parsed and inserted with no problem.
我在Apache 2.2的使用Django 1.5与2.7蟒蛇与安装在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
更新
我在做一些维护的服务器上并重新启动......一旦它回来了code工作就好了......这实际上使我只是担心一样的...
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的一个bug。
i think it's a bug on mod_wsgi.
不管怎样,谢谢!
推荐答案
遇到这种行为,并用了一个过滤器,然后如预期更新的作品。例如:
Encountered this behavior and used a "filter" then update works as expected. For example:
Students.objects.select_for_update().filter(id=3).update(score = 10)
仅供参考:除非你正在处理交易,分别修改每个字段使用保存()
可能会在多线程环境中创建的数据不一致。通过调用的ThreadA 保存时间()
一个模型,另一个threadB可能已经改变了模型字段并保存。在这种情况下的ThreadA具有读取更新的模型和变化。
Just FYI: Unless you are handling transactions, modifying each field separately using save()
might create data inconsistency in a multi-threaded environment. By the time threadA calls save()
on a model, another threadB could have changed the model fields and saved. In which case threadA has to read the updated model and change.
这是在Django的1.6.2
This was on Django 1.6.2
这篇关于Django的错误&LT;模型&GT;对象有没有属性'更新'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!