djangodjango-modelsdjango-ormdjango-inheritancemulti-table-inheritance

id of object is none after save in django


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?


Solution

  • 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.