Repository navigation
Save many metafields with a single request #112
Description
Activity
Active resource does a lot of work to add methods to add attributes etc but at the end of the day it just sends json to the server. If you either post the json directly not using the library or edit the attributes directly you should be able to do this.
also see discussion on #76
Based on the discussion in #76, I created the following helper function:
def attach_metafields(target, namespace, dictionary): mfs = [] for key in dictionary.keys(): value = str(dictionary[key]) if type(dictionary[key]) is int: value_type = "integer" else: value_type = "string" mf_dict = { "namespace" : namespace, "key" : key, "value" : value, "value_type" : value_type, } mfs.append(mf_dict) if hasattr(target.metafields, '__call__'): target.metafields = mfs elif type(target.metafields) is list: target.metafields.extend(mfs)
After saving the target, however, the metafields are not saved on the server. I am attempting this on an existing product. Any idea why it's not working?
Got it to work. I had to attach it to attributes. Here's the updated function:
def attach_metafields(target, namespace, dictionary): mfs = [] for key in dictionary.keys(): value = str(dictionary[key]) if type(dictionary[key]) is int: value_type = "integer" else: value_type = "string" mf_dict = { "namespace" : namespace, "key" : key, "value" : value, "value_type" : value_type, } mfs.append(mf_dict) if "metafields" in target.attributes.keys(): target.attributes["metafields"].extend(mfs) else: target.attributes["metafields"] = mfs
if you add tests and make a PR I think I can accept this!
I apologize, I'm not very familiar or clear with how this module/pyactiveresource actually work. Is there any one place I can add this so that every resource that supports metafields automatically has access to it? Or does this need to be added to every resource class manually?
Also, I feel like the level of abstraction that my helper function goes is a bit beyond the level abstraction than most of the other functions in this module. Most other functions can be mapped 1-to-1 to the json API documentation, which is useful because this effectively acts as documentation for this module. The functions/attributes that do not have this 1-to-1 mapping are always frustrating, because their existence/usage are non-obvious, requiring me to dig around in the source code and/or futz around to figure out how they work. As useful as this function is, I don't want to contribute to this problem. If you think this isn't actually a problem, I'd love to hear you address these issues (or non-issues).
I'd also love to hear other people's opinions (particularly @gavinballard's) about all this.
Its a valid point - it will be hard for developers to know about the existence of this function since this lib itself is not really documented only the api is.
Another issue - I don't know if adding metafields like this even works for other resources it might only work for products.
It might be better to just leave as an issue and then people can find it this way?
I just confirmed that this function works for products, images, variants, and customers (did not try the others yet), however with a peculiarity:
You can only create metafields, not update. If any metafield you pass exists, none of them are created or updated, the following error is received:
must be unique within this namespace on this resourceI think this is because it is using a
POSTrequest when it needs to bePUT. I noticed that all resources have a .put() method, however I'm not sure how it works?Something I'm still confused about, relating to my OP:
p = shopify.Product.find()[0] print len(p.metafields()) > 50 print p.attributes.get("metafields") > None p.id = None p.images = None p.save() print len(p.metafields()) > 50 p.save() print p.errors.full_messages() > []
In the above example, you can see that a new product was saved with metafields, even though "metafields" was not in attributes. This means that the metafields are being stored somewhere else in the object, and in a place where it doesn't raise an error if the object is saved when they already exist. As per my original question in the OP: where is this metafield information stored? I can't figure it out from the source code.
that is certainly interesting. I'd have to look into it as well since I am not sure how pyactiveresource is doing this either
I don't know what happened, but it's not working anymore. When I save a product with
id = None, the metafields are no longer being saved along with it, unless I reassign them like so:product.attributes["metafields"] = product.metafields()
I'm not sure if I'm crazy or if something changed...
Edit: just to clarify, the helper function above still works.
Thanks for contributing! This package is deprecated and we're archiving this repo, so we're closing all open issues and PRs. Please use shopify-app-python instead, and open new issues there. More context: Rethinking support for PHP/Python packages
There does not seem to be a way to attach metafields locally, and then simply save the product with the metafields along with it. I'm forced to use add_metafields() or Metafield().save(), both of which adds a single metafield per API call. This is very slow.
If I take a product with 50 metafields, and save it as a new product, it saves almost instantly, like so:
How can I attach metafields to a product in the same way they are attached when they are fetched?