id of object is none after save in django

django models
django model fields

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?


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.

#8799 (.save() and .objects.create() does not set the primary key , .save() and .objects.create() does not set the primary key when the model has an CREATE TABLE "Person" ( "id" integer NOT NULL PRIMARY KEY, "name" text AutoField , Django doesn't know that it needs to update the field after saving. For ForeignKey objects, this method accepts a bulk argument to control how to perform the operation. If True (the default), QuerySet.update() is used. If bulk=False, the save() method of each individual model instance is called instead. This triggers the pre_save and post_save signals and comes at the expense of performance.


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

Model instance reference | Django documentation, Model): def refresh_from_db(self, using=None, fields=None, **kwargs): b2. save() >>> b2.id # Returns the ID of your new object. Emit a post-save signal. 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.


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

#10339 (models.my_model.objects.get(id=None) returns a result in , I stumbled upon an interesting bug today, and I believe that I've narrowed it down to Django's MySQL layer. When using MySQL, models.my_model.objects.get(id= None) will return a result, from django.db import models class company( models. c.save() >>> models.company.objects.all() [<company: company object>] >� Unfortunately, there isn’t a workaround when creating or updating objects in bulk, since none of save(), pre_save, and post_save are called. Executing custom SQL ¶ Another common pattern is writing custom SQL statements in model methods and module-level methods.


#10811 (Assigning unsaved model to a ForeignKey leads to silent , Assigning an object to a ForeignKey field without first saving it leads to silent, File "/Users/myk/Documents/dev/django/django/test/client.py", line 460, in post out id of the ET object before and after save is called but it comes out None . Calling save() on a new session may also work but has a small chance of generating a session_key that collides with an existing one. create() calls save() and loops until an unused session_key is generated. If you’re using the django.contrib.sessions.backends.db backend, each session is a normal Django model


Models | Django documentation, CREATE TABLE myapp_person ( "id" serial NOT NULL PRIMARY KEY, If you change the value of the primary key on an existing object and then save it, a new members of the Beatles that joined after 1 Jan 1961 >>> Person.objects.filter(� Just change the primary key of your object and run save (). obj = Foo.objects.get (pk=<some_existing_pk>) obj.pk = None obj.save () If you want auto-generated key, set the new key to None. Hope this is helpfull!!


save - django - Python documentation, b2.save() >>> b2.id # Returns the ID of your new object. Emit a post-save signal. If the object's primary key attribute is set to a value that evaluates to True (i.e., a value other than None or the empty string), Django executes an UPDATE . So Update view must display the old data in the form and let user update the data from there only. Django provides extra-ordinary support for Update Views but let’s check how it is done manually through a function-based view. This article revolves around Update View which involves concepts such as Django Forms, Django Models.