Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在get_或create中使用另一个对象更新对象?_Python_Mysql_Django - Fatal编程技术网

Python 如何在get_或create中使用另一个对象更新对象?

Python 如何在get_或create中使用另一个对象更新对象?,python,mysql,django,Python,Mysql,Django,我必须创建具有类似字段的表,并且要将对象从一个表复制到另一个表。 问题是第二个表中可能缺少对象,所以我必须使用get\u或\u create()方法: 请建议使用比上面更优雅的方式您可以尝试这样的方式 def copy_fields(frm, to): id = to.id for field in frm.__class__._meta.fields: setattr(to, field.verbose_name, field._get_val_from_obj

我必须创建具有类似字段的表,并且要将对象从一个表复制到另一个表。 问题是第二个表中可能缺少对象,所以我必须使用get\u或\u create()方法:


请建议使用比上面更优雅的方式

您可以尝试这样的方式

def copy_fields(frm, to):
    id = to.id
    for field in frm.__class__._meta.fields:
        setattr(to, field.verbose_name, field._get_val_from_obj(frm))
    to.id = id

您可以在字段名称上循环,并在另一个
产品
实例中更新它们:

for new_product in new_products_list:
    # use different variable names, otherwise you won't be able to access
    # the item from new_product_list here
    product, created = Products.objects.get_or_create(article=new_product.article)

    if not created:
       for field in new_product._meta.get_all_field_names():
           setattr(product, field, getattr(new_product, field))
       product.save()

这类似于Ashwini Chaudhary,尽管我认为它会处理您在评论中提到的错误

new_products_list= (
#   obj1, obj2, obj3 would be from [<NewProduct: EEEF0AP>, <NewProduct: XR3D-F>,<Product: XXID-F>] in your question
#   NewProduct would just be the model that you import
#   NewProduct._meta.fields would be all the fields
    (obj1, NewProduct, NewProduct._meta.fields,),
    (obj2, NewProduct, NewProduct._meta.fields,),
    (obj3, NewProduct, NewProduct._meta.fields,),
)

for instance, model, fields in new_products_list:
    new_fields = {}
    obj, created = model.objects.get_or_create(pk=instance.article) # this is pretty much just here to ensure that it is created for filter later
    for field in fields:
        if field != model._meta.pk: # do not want to update the pk
            new_fields[field.name] = request.POST[field.name]
    model.objects.filter(pk=question_id).update(**new_fields) # you won't have to worry about updating multiple models in the db because there can only be one instance with this pk
新产品列表=(
#在你的问题中,obj1,obj2,obj3来自[,]
#新产品就是您导入的模型
#NewProduct.\u meta.fields将是所有字段
(obj1,NewProduct,NewProduct.\u元字段,),
(obj2,NewProduct,NewProduct.\u元字段,),
(obj3,NewProduct,NewProduct.\u元字段,),
)
例如,模型、新产品列表中的字段:
新的_字段={}
obj,created=model.objects.get_或_create(pk=instance.article)#这里的内容主要是为了确保为以后的筛选创建它
对于字段中的字段:
如果是字段!=型号。_meta.pk:#不想更新pk
新字段[field.name]=request.POST[field.name]
model.objects.filter(pk=question_id).update(**new_fields)#您不必担心更新数据库中的多个模型,因为只有一个实例具有此pk

我知道这是一个多月前的事了,但我想我会分享我的解决方案,即使你已经弄明白了

那太好了,但是当我输入
product.save()
时,我得到了这样一个错误
IntegrityError:(1062,“重复输入'2326209_FARNELL'作为键'product_article_4b591bee1bc47d02_uniq')
您的新产品中可能有一个字段包含重复的值。文章字段中出现错误,但它是唯一的,因此引发该错误,因为ORM试图创建相同的对象,而不是更新现有的对象。那么,我如何才能真正触发更新而不是创建?
for new_product in new_products_list:
    # use different variable names, otherwise you won't be able to access
    # the item from new_product_list here
    product, created = Products.objects.get_or_create(article=new_product.article)

    if not created:
       for field in new_product._meta.get_all_field_names():
           setattr(product, field, getattr(new_product, field))
       product.save()
new_products_list= (
#   obj1, obj2, obj3 would be from [<NewProduct: EEEF0AP>, <NewProduct: XR3D-F>,<Product: XXID-F>] in your question
#   NewProduct would just be the model that you import
#   NewProduct._meta.fields would be all the fields
    (obj1, NewProduct, NewProduct._meta.fields,),
    (obj2, NewProduct, NewProduct._meta.fields,),
    (obj3, NewProduct, NewProduct._meta.fields,),
)

for instance, model, fields in new_products_list:
    new_fields = {}
    obj, created = model.objects.get_or_create(pk=instance.article) # this is pretty much just here to ensure that it is created for filter later
    for field in fields:
        if field != model._meta.pk: # do not want to update the pk
            new_fields[field.name] = request.POST[field.name]
    model.objects.filter(pk=question_id).update(**new_fields) # you won't have to worry about updating multiple models in the db because there can only be one instance with this pk