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

Error in saving a model with fk value instead of related model. #270

Closed
collerek opened this issue Jul 14, 2021 · 1 comment
Closed

Error in saving a model with fk value instead of related model. #270

collerek opened this issue Jul 14, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@collerek
Copy link
Owner

You should be able to pass the underlying value, so course = await Course.objects.create(department=1) should work.
What happens in your case?

I see this error:
AttributeError: 'int' object has no attribute 'get'

post_info = {
    "title": "My post",
    "link": 1
}

self.post = await Post.objects.create(**post_info)

# Models
class Post(ormar.Model):
    id: int = ormar.Integer(primary_key=True)
    title: str = ormar.String(max_length=500)
    link: PageLink = ormar.ForeignKey(PageLink, related_name='posts', ondelete="CASCADE")

    class Meta(BaseMeta):
        tablename = 'posts'

class PageLink(ormar.Model):
    id: int = ormar.Integer(primary_key=True)
    value: str = ormar.String(max_length=2048)
    country: str = ormar.String(max_length=1000)

    def __str__(self):
        return f'{self.id}'

    class Meta(BaseMeta):
        tablename = 'pagelinks'

Stack:

  File ".../venv/lib/python3.9/site-packages/ormar/queryset/queryset.py", line 1018, in create
    new_kwargs = self.model.prepare_model_to_save(new_kwargs)
  File ".../venv/lib/python3.9/site-packages/ormar/models/mixins/save_mixin.py", line 40, in prepare_model_to_save
    new_kwargs = cls.substitute_models_with_pks(new_kwargs)
  File ".../venv/lib/python3.9/site-packages/ormar/models/mixins/save_mixin.py", line 130, in substitute_models_with_pks
    model_dict[field] = field_value.get(target_pkname)
AttributeError: 'int' object has no attribute 'get'

Originally posted by @bilyalov in #262 (reply in thread)

@collerek collerek added the bug Something isn't working label Jul 14, 2021
collerek added a commit that referenced this issue Jul 21, 2021
@collerek collerek mentioned this issue Jul 21, 2021
@collerek
Copy link
Owner Author

Fixed by #280

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant