id of object is none after save in django
V

4

6

I'm looping through a list of objects and saving. I need the newly generated id or pointer id right after the save but it is None.

Here is my code:

for category in category_list:
      saved_category = category.save()
      print saved_category.parentCategory_ptr_id      
      print saved_category.id

This saves my object after the routine is run, but again, does not give me the id at this line.

here is my model:

class ParentCategory(models.Model):
    name = models.CharField(max_length=255)

class Category(ParentCategory):
    description = models.CharField(max_length=255)

category list was created like so:

category_list = []
    for row in value_list:
        category = Category(description=row.description)
        category_list.append(category)


 return category_list

What am I doing wrong?

Valeda answered 30/11, 2013 at 19:16 Comment(7)
How was object_list created?Subscribe
@Subscribe I added the code for object listValeda
"category" has a lower case "c"… is it a typo?Lubbock
@Lubbock yes it was.Valeda
can you show how value_list was created? or give us some context what's the process here? What is returning category_list and where does the first function go? Because frankly, I believe the trouble is you don't save the category objects to begin with, so upon arriving to the second function (with save_category) they stop existing (I might be entirely wrong, I just don't have enough context here)Nalda
@Nalda this is pulled from a CSV file. The actual category object saves in the database when the entire routine runs, it is just that I cannot get the id at the line above.Valeda
Possible duplicate of Django Model is saved, but returns NoneInge
V
1

The problem is with:

saved_category = category.save()

It needs to be:

category = category.save()

The original saved object in the list is the object that contains the id.

Valeda answered 2/12, 2013 at 18:19 Comment(3)
Probably better as just: category.save() No need to reassign.Bullwhip
can you explain more as to why saved_category = category.save() won't work ?Sismondi
reassign give me an error: NoneType object (i use django 1.11)Bespoke
I
1

Their is no need of reassign.

category.save()

When you call save() method the object is saved into database and assign id (primary key) to the object. Saving Objects Django Official

Other same question asked on Stackoverflow and correct answer by Daniel Roseman

Inge answered 29/11, 2019 at 4:19 Comment(0)
B
0

I don't think the object will get saved. Seems that the object you are creating lacks sufficient data to satisfy db constraints. You might have used try-catch somewhere, you would have seen the error. Try adding blank=True, null=True to name in ParentCategory or provide a name while creating the object. I hope this works...

Broddy answered 2/12, 2013 at 18:27 Comment(0)
L
0

I had a similar problem, but in my case it was that I had in my model the id field redefiend as id = models.Integer(primary=key=True). This was causing the id to be always None after saving. If this is your case, just remove this field, call ./manage.py makemigrations &&./mnage.py migrate and test your application.

Livelihood answered 24/6 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.