Comments (10)
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.
from shopify_python_api.
also see discussion on #76
from shopify_python_api.
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?
from shopify_python_api.
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
from shopify_python_api.
if you add tests and make a PR I think I can accept this!
from shopify_python_api.
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.
from shopify_python_api.
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?
from shopify_python_api.
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 resource
I think this is because it is using a POST
request when it needs to be PUT
. 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.
from shopify_python_api.
that is certainly interesting. I'd have to look into it as well since I am not sure how pyactiveresource is doing this either
from shopify_python_api.
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.
from shopify_python_api.
Related Issues (20)
- When is 2023-7 api version releasing HOT 6
- pyactiveresource.connection.Error: Not Found: fulfillments.json when creating fulfillment HOT 5
- Add support to AssignedFulfillmentOrder HOT 2
- Running into certificate issue with python 3.12 HOT 3
- A new release of the Shopify API is due soon HOT 2
- Test failure for `test_raises_if_aud_doesnt_match_api_key` HOT 2
- A new release of the Shopify API occurred
- API Version is not working greater then 2023-04 HOT 12
- Deprecation Notice for `gateway` Field in Order Object - Impact on Shopify Python SDK HOT 2
- Package Version 12.4.0 shows released in CHANGELOG but not available in PyPi HOT 4
- 413 (Payload Too Large) while uploading images under 2Mb HOT 2
- Add promised online access token support HOT 2
- Allow to interact console with ipython
- Global state for sessions causes race conditions on async code HOT 2
- A new release of the Shopify API is due soon HOT 2
- I cannot find the abandoned checkouts endopoint HOT 5
- Shopify Python SDK Deletes Customer Address on Same Address Update HOT 4
- A new release of the Shopify API occurred HOT 2
- request online access token HOT 2
- `calculated_refund` Appears as `None` when Printed but is Not Actually `None` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shopify_python_api.