模型匹配查询不存在

时间:2018-01-17 10:21:54

标签: python django django-models

我收到了错误:

WARNING:logger:invoice_settings_create_new_invoice ERROR: Invoice matching query does not exist.

这是我在尝试为对象调用.save()方法时收到的错误:

object = Invoice(name=so_name)
for fieldname, fieldvalue in invoice_data['fields'].items():
    if fieldvalue['type']=='relto':
        model_rel = object._meta.get_field(fieldname).rel.to
        setattr(object, fieldname, model_rel().objects.get(pk=int(fieldvalue['value'])))
    else:
        if (object._meta.get_field(fieldname).get_internal_type() == 'IntegerField'):
            setattr(object, fieldname, int(fieldvalue['value']))
        if (object._meta.get_field(fieldname).get_internal_type() == 'DecimalField'):
            setattr(object, fieldname, float(fieldvalue['value']))
        if (object._meta.get_field(fieldname).get_internal_type() == 'DateField'):
            setattr(object, fieldname, datetime.datetime.strptime(fieldvalue['value'], "%d.%m.%Y").date())
object.save()

我没有使用复杂的东西。我创建了新对象。设置对象字段的值。试图保存它。并收到错误。 为什么会这样?我怎么解决它?

UPD

.save()之前的修改代码:

logger.warning('object name: %s', object.name)
logger.warning('object fk: %s', object.tender_game_id.year_number)
object.save()

错误:

WARNING:logger:object name: INV-5542-2017/OP
WARNING:logger:object fk: 5542
WARNING:logger:invoice_settings_create_new_invoice ERROR: Invoice matching query does not exist.

UPD2

如果我只创建对象Invoice并尝试保存它,我会收到一些错误:

object = Invoice(name=so_name)
logger.warning('object name: %s', object.name)
object.save()

错误:

WARNING:logger:object name: INV-5542-2017/OP
WARNING:logger:invoice_settings_create_new_invoice ERROR: Invoice matching query does not exist.

1 个答案:

答案 0 :(得分:0)

此错误是由于其他开发人员编辑的代码造成的。他为Invoice()模型修改了方法save()。