2013-03-08 96 views
10

UPDATEDjango的错误<model>对象有没有属性“更新”

我在做一些维护的服务器上并重新启动......一旦它回来的代码工作就好了......这实际上使我担心一样的...

我认为这是在mod_wsgi的一个bug。

无论如何,谢谢!

我真的很新的Django(昨日开始)。我设法使用xlrd做一个excel解析器,一切工作正常与数据(它加载真的非常快),我需要更新数据库中的文件信息,所以我可以知道如何负载,这是我的地方问题,save()方法不起作用,我已经使用update和get和filter,但总是出现同样的问题。

我希望你能指出我在哪里是错误

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}) 

在##开始更新# #块我已经尝试过

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) 

我找不到这个错误还是其他什么东西在模型文件中添加任何引用,我敢肯定它的东西很容易修复,但我无法找到它。

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

异常类型:AttributeError的在/ PROCESA/4

异常值: '档案馆' 对象没有属性“更新“

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

我在Apache 2.2的使用Django 1.5与2.7蟒与安装在EC2上亚马逊的mod_wsgi和MySQL后端

UPDATE 我是做服务器上的一些维修和重新启动......一旦它来了返回的代码工作就好了......这实际上让我担心一样的...

我认为这是在mod_wsgi的一个bug。

无论如何,谢谢!

+0

你重写你未在模型中定义的更新。重写你使用更新方法的代码或在你的模型中定义更新方法,以便你可以使用它 – catherine 2013-03-09 00:29:38

回答

1

我没有通过所有的代码去了,但此行:

infoarchivo2 = archivo.objects.filter(archivo_id = procesar) 

不会从数据库返回实例或对象,它返回一个QuerySet,即使查询集只是有一个元素。你将不得不迭代Queryset oy或许改变get方法过滤器。

只要方法更新,我不认为它的实施。

2

我有过类似的情况,但使用时,建设工作一样:

this_spot = Spot.objects.filter(pk=obj.spot.pk) 
this_spot.update(friendly_rate=rating_to_be_persisted) 

但是从外键端类的情况下,不能正常工作,在这里我想直接访问单个实例,e.g。退回'Spot' object has no attribute 'update'

原因很简单,在django documentation描述update()的工作方式:

周围是像Django的网站上显示方法的方式:

>>> b = Blog.objects.get(pk=1) 

# Update all the headlines belonging to this Blog. 
>>> Entry.objects.select_related().filter(blog=b).update(headline='Everything is the same') 
2

遇到这样的行为和使用“过滤器”,然后更新按预期工作。例如:

Students.objects.select_for_update().filter(id=3).update(score = 10) 

仅供参考:除非你是处理事务,修改分别使用save()可能会在多线程环境中创建的数据不一致的每个字段。当threadA在模型上调用save()时,另一个threadB可能会更改模型字段并保存。在这种情况下,threadA必须读取更新的模型并进行更改。

这是在Django的1.6.2

2

您还可以使用update_fields此:

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

这是非常好的答案,最重要的是它非常清晰 – 2016-11-03 13:18:07

相关问题