question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model no attribute 'id' after creating

See original GitHub issue

Describe the bug

Storing a new model does not allow to access the id attribute. Accessing in code after save() results in a class model 'Category' has no attribute id

category.save()
request.session.flash('success', f'Saved: {category.id}')
return request.redirect_to('admin.category.table')

To Reproduce

create a simple store function:

from app.Category import Category

class CategoryController(Controller):

    def store(self, request: Request):
        category = Category()
        category.name = request.input('name')
        category.slug = request.input('slug')
        category.description = request.input('description')
        category.sorting = request.input('sorting')
        category.save()
        return category

Expected behavior What do you believe should be happening?

{
  "id": 1,
  "name": "test",
  "slug": "test",
  "description": "test",
  "sorting": "1"
}

Actual behavior

{
  "name": "test",
  "slug": "test",
  "description": "test",
  "sorting": "1"
}

Screenshots or code snippets What database are you using?

  • Type: SQLite 2.8.17
  • Masonite ORM: masonite-orm==1.0.27

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
josephmancusocommented, Feb 16, 2021

@resmo releasing 1.0.29 now to fix this. Thanks for the report!

0reactions
josephmancusocommented, Feb 16, 2021

@Marlysson @resmo is correct, His code should be working as he is intending it to work. I think the issue is we are just missing a fill.

            if self.is_loaded():
                result = builder.update(self.__dirty_attributes__)
            else:
                result = self.create(self.__dirty_attributes__, query=query)
            self.observe_events(self, "saved")
            self.fill(result) # <--- this is missing here. Assigns the result to the current model
            return result
Read more comments on GitHub >

github_iconTop Results From Across the Web

'model' object has no attribute 'id' - python - Stack Overflow
You've explicitly told Django that filmName is the primary key, so there is no separate ID. It's not usually a good idea to...
Read more >
type object 'MyModel'' has no attribute 'id' - Google Groups
Exception Type: AttributeError at /support/helping/product/1/1. Exception Value: type object 'ProductModel' has no attribute 'id'. #----model
Read more >
704 (AttributeError with non-standard primary key name)
... pk) AttributeError: 'Machine' object has no attribute 'id'. It does recognize it properly when generating SQL. "django-admin.py sql" gives: CREATE TABLE ...
Read more >
Error on automatic primary key fields - model has no attribute ...
Based on the Typechecking models and queryset section of the tutorial, I do not expect error: "BlogPost" has no attribute "id" , and...
Read more >
AttributeError: '_unknown' object has no attribute 'id'-- | Odoo
When I install the module the first time on clean database, i can create a record, but next times or if i uninstall...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found