2013-03-08 94 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

相關問題