Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_link() breaks if contentful type has field named 'type' #92

Open
MikeFE opened this issue Aug 23, 2023 · 2 comments
Open

to_link() breaks if contentful type has field named 'type' #92

MikeFE opened this issue Aug 23, 2023 · 2 comments

Comments

@MikeFE
Copy link

MikeFE commented Aug 23, 2023

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.

@flow-ryan
Copy link

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"

@rubydog
Copy link
Collaborator

rubydog commented Feb 28, 2024

Hi @MikeFE,

Your solution appears reasonable. Could you please open a PR for us to review?

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants