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

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

问题描述

更新



我在服务器上进行一些维护并重新启动...一旦代码工作正常,实际上让我担心的只是一样的...



我认为这是mod_wsgi的错误。



谢谢,无论如何!



我真的很喜欢django(昨天开始)。我设法使用xlrd创建一个excel解析器,一切都可以正常使用数据(它真的很快加载),我需要更新数据库中的文件信息,所以我可以知道如何负载,这是我有问题,save()方法不起作用,我已经用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

从procesa.models导入* 
从django.conf导入设置
从django.shortcuts import render_to_response
import xlrd
from time import strftime
from symbol import except_clause
def procesa(request,procesar = 0):
datos =无
infoarchivo =无
if(procesar> 0):
try:
infoarchivo = archivo.objects.get(archivo_id = int(procesar))
除了:
返回render_to_response('error.html')

if(infoarchivo不是无):
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。替换(。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_d

如果(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 ##块我已经尝试过

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

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

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

我找不到任何这个错误的引用或其他添加到模型文件中,我很确定这是一个很容易修复,但我只是可以'



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



异常类型:/ procesa / 4中的AttributeError



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

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



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



更新
我在服务器上进行一些维护,重新启动...一旦它回来了代码工作ked只是很好...这实际上让我担心只是一样的...



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



无论如何感谢

解决方案

我有类似的情况, p>

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

但不能工作,如果我想直接访问单一实例,例如从外键侧类访问。返回'Spot'对象没有属性'update'



原因只是方式 update() django文档



方法就像在django站点上显示的方式:

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

#更新属于此博客的所有标题。
>>> Entry.objects.select_related()。filter(blog = b).update(headline ='Everything is a same')


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...

i think it's a bug on mod_wsgi.

Thanks anyway!

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

and

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

and

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

Exception Type: AttributeError at /procesa/4

Exception Value: 'archivo' object has no attribute 'update'

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

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...

i think it's a bug on mod_wsgi.

Thanks anyway!

解决方案

I have had similar case, but it worked when using construction like:

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

but not working in case, where I wanted access directly single instance, e.g from foreign key side class. Returning 'Spot' object has no attribute 'update'.

The reason is simply the way update() works described in django documentation:

The way around is approach like shown on the django site:

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

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

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