metafields python API

isaacdvory
Tourist
16 0 2

Hello,

I'm using this git project: https://github.com/Shopify/shopify_python_api   to use shopify api.

I'm trying to upload a product with metafields, but I can't get it to work, everything else is working great. 

this is what I'm doing: 

product = shopify.Product()
product.metafields = [{
"key": "long_description",
"value": "test",
"value_type": "string",
"namespace": "prd_long_description"
}]

 

of course, filling out all other needed info, doing product.save() , but no metafield...

Can someone help me with this please? if not possible to do on product upload, I don't mind doing it with another call , but I couldn't find a way to add metafield with that project to a specific product...

Thank you!

Replies 3 (3)

Andreas_S
Shopify Partner
7 0 0

Currently also looking for this functionality... Hence, stumbled over your posting. As the documentation of the Python API project has some room for improvement, I tend to read the source. Which is quiet descriptive:

In your IDE click on shopify.Product, opens the Products source file, which contains

class Product(ShopifyResource, mixins.Metafields, mixins.Events):

Click on mixins.Metafields opens Metafiedls object, which contains the method def add_metafield(self, metafield):

Hence, try somthing like:

product.add_metafield({
"key": "long_description",
"value": "test",
"value_type": "string",
"namespace": "prd_long_description"
})

Hope this helps.

But take care: If there is an error, ther is no exception thrown. This might be normal Ruby ActiveRecord behavior, but did cost me some hours to recocnise there is a problem at all!

Better check the Metafield.errors or Product.errors object for success or error.

In my opinion, it's never a very clever idea to port one language common behavior to an other language, where nobody expects it.

isaacdvory
Tourist
16 0 2

Thanks Adnreas.

This is what ended up working for me: 

product.add_metafield(shopify.Metafield({
        'key': 'long_description',
        'value': 'test',
        'value_type': 'string',
        'namespace': 'prd_long_description'
    }))

I'll have to do it after the product has been saved. 

When I try yours (before product is saved) I get this error: 

shopify/mixins.py", line 26, in add_metafield
    raise ValueError("You can only add metafields to a resource that has been saved")
ValueError: You can only add metafields to a resource that has been saved
 

and I also get an error when trying it after : 

shopify/mixins.py", line 28, in add_metafield
    metafield._prefix_options = dict(resource=self.__class__.plural, resource_id=self.id)
AttributeError: 'dict' object has no attribute '_prefix_options'
 

Unfortunately, with the way I found, I need to do an API call for each metafield I want to add (and I have 7), so that's kind of sucks... I was hopeing to find a way to do it before the product save, like shoopify API documentation shows. 

"documentation of the Python API project has some room for improvement" - that's an understatment 🙂 

 

Thanks anyway. 

 

 

 

Andreas_S
Shopify Partner
7 0 0

😄 You cought me! I postet my answer, before I tested it itself. And we both got bitten for my hurry...

I edited my last post, to reflect my findings. But your post turned me into the right direction 😉

Thanks.