contentful-management.py
contentful-management.py copied to clipboard
to_link() breaks if contentful type has field named 'type'
I've run into a problem where I'm trying to assign an Entry returned from environment.entries().find() to a reference field in another Entry.
The problem is it seems if I have type as a field name in Contentful, it's running into issues when it eventually calls to_link() here: https://github.com/contentful/contentful-management.py/blob/master/contentful_management/resource.py#L153
I think because the __getattr__ is returning the regular field type as a higher priority to the sys field type. So the link_type is getting assigned whatever the regular field type was set to, which doesn't make sense in the context of Links. One solution is of course to just never use a field called type in our schema, but perhaps we could change the line to:
link_type = self.link_type if self.sys['type'] == 'Link' else self.sys['type']
That way it won't rely on __getattr__ and will be guaranteed to get the proper sys type. What do you think? I can create a PR if necessary.
I think I have the same issue.
I want to assign a link to field with
entry.fields()["some_link_field"] = other_entry.to_link().json()
but get a validation error because the "other_entry" object has a field called "type"
Hi @MikeFE,
Your solution appears reasonable. Could you please open a PR for us to review?
Cheers