Why can't erase this model object? "AssertionError: Question object can't be deleted because its id attribute is set to None."

4,330 views
Skip to first unread message

Chris Seberino

unread,
Jan 20, 2012, 10:49:20 PM1/20/12
to Django users
I get the following error when attempting to delete an object of my
model...

"AssertionError: Question object can't be deleted because its id
attribute is set to None."

The class definition is thus...

class Question(django.db.models.Model):
"""
Represents a database table.
"""

subject = django.db.models.CharField(max_length = 1000)
section = django.db.models.CharField(max_length = 1000)
text = django.db.models.CharField(max_length = 1000)
answer = django.db.models.CharField(max_length = 1000)
answer_type = django.db.models.CharField(max_length = 1000)

Tom Evans

unread,
Jan 21, 2012, 7:44:21 AM1/21/12
to django...@googlegroups.com
On Sat, Jan 21, 2012 at 3:49 AM, Chris Seberino <cseb...@gmail.com> wrote:
> I get the following error when attempting to delete an object of my
> model...
>
> "AssertionError: Question object can't be deleted because its id
> attribute is set to None."
>

Models are representations of rows in a database.
Deleting a model instance implies removing a row from a database.
Model instances have an id attribute which denotes their row in the database.
Models whose id is None don't exist in the database, and so it is
nonsense to delete them - they don't exist.

Cheers

Tom

Chris Seberino

unread,
Jan 21, 2012, 12:31:32 PM1/21/12
to Django users

On Jan 21, 6:44 am, Tom Evans <tevans...@googlemail.com> wrote:
> Models are representations of rows in a database.
> Deleting a model instance implies removing a row from a database.
> Model instances have an id attribute which denotes their row in the database.
> Models whose id is None don't exist in the database, and so it is
> nonsense to delete them - they don't exist.

That makes perfect sense but then why do these zombie id=None objects
show up?
*WHERE* are they living if not in the database? Does the Django shell
somehow have a secondary storage area for this zombie id=None object
stuff I can delete somehow?

JohnA

unread,
Jan 21, 2012, 3:43:48 PM1/21/12
to Django users
How are you finding these objects? That might point to the answer.

Probably the likeliest explanation is that you are creating the
objects but not saving them.

Chris Seberino

unread,
Jan 21, 2012, 6:39:58 PM1/21/12
to Django users
On Jan 21, 2:43 pm, JohnA <john.armstrong....@gmail.com> wrote:
> How are you finding these objects?  That might point to the answer.
>
> Probably the likeliest explanation is that you are creating the
> objects but not saving them.

In the Django shell I do

quests = Question.objects.all()
quests[579].delete()

Why does the objects.all() invocation add this id=None crud to quests
list?

cs

JohnA

unread,
Jan 21, 2012, 9:49:41 PM1/21/12
to Django users
Is your code really that simple - I mean get the queryset and
immediately try to delete something out of it? If so, maybe it's time
to step through the delete in the debugger. Querysets are lazy but I
assume that referencing an item in the queryset kike you do will cause
the actual data to get feteched. Maybe the problem is trying to
delete something directly out of the queryset, or maybe you're somehow
deleting the same record twice, or maybe django is somehow getting
confused while performing the operation.

-- John

akaariai

unread,
Jan 22, 2012, 7:34:04 AM1/22/12
to Django users
There is still the possibility that you really have a row where the PK
is somehow null. That would need an investigation through SQL. If so,
there is an error somewhere, either in your database schema, or maybe
in Django itself.

- Anssi

bruno desthuilliers

unread,
Jan 23, 2012, 7:25:21 AM1/23/12
to Django users
On Jan 21, 6:31 pm, Chris Seberino <cseber...@gmail.com> wrote:
> On Jan 21, 6:44 am, Tom Evans <tevans...@googlemail.com> wrote:
>
> > Models are representations of rows in a database.
> > Deleting a model instance implies removing a row from a database.
> > Model instances have an id attribute which denotes their row in the database.
> > Models whose id is None don't exist in the database, and so it is
> > nonsense to delete them - they don't exist.

This should read "they don't exist _in the database_" - a model
instance is not a database row, it's a python object. The database row
only exists if and when the model instance has been saved, and until
the database row is deleted.

> That makes perfect sense but then why do these zombie id=None objects
> show up?

That's one question... And I don't have the answer.

> *WHERE* are they living if not in the database?  Does the Django shell
> somehow have a secondary storage area for this zombie id=None object
> stuff I can delete somehow?

And that's another question, which has a very simple answer: model
instances live in your python process (django's dev server, wsgi
process, custom management command, django shell or whatever). If you
fire a django shell, import your model class and instanciate it
directly and don't save it you will have a model instance with no id
(assuming you're using the auto id field which is the default).


bruno desthuilliers

unread,
Jan 23, 2012, 7:27:03 AM1/23/12
to Django users


On Jan 22, 1:34 pm, akaariai <akaar...@gmail.com> wrote:
> There is still the possibility that you really have a row where the PK
> is somehow null.

Very unlikely given the model definition, but well, sh*t happens.

bruno desthuilliers

unread,
Jan 23, 2012, 7:32:41 AM1/23/12
to Django users
No reason it should do this, and I really doubt this is the case -
unless you're using a (broken) custom Manager you didn't mention when
you posted your model definition.


> to quests list?

"quests" is not a list, it's a QuerySet (https://
docs.djangoproject.com/en/1.3/ref/models/querysets/).

Petr Přikryl

unread,
Jan 23, 2012, 1:30:41 PM1/23/12
to django...@googlegroups.com

I am a Django novice, so only a wild guess based on my Python knowledge...

 
> In the Django shell I do
>
> quests = Question.objects.all()
> quests[579].delete()
>
> Why does the objects.all() invocation add this id=None crud
> to quests list?
 
Are you sure that you have 580 items in the container?
A Django program is to be executed at the web server side.
Therefore, it is desirable that the program does not crash
very easily.  Search for the information on how the quests
is implemented.  It is likely that it is not a "normal", strict
container that react by raising the exception.  Returning
the special objects for the range outside the real data space
may be a good way to let you test further if the object really
exist.  My guess is that you do not have that many object.
(But I may be completely wrong ;)
 
Petr

Leandro Ostera Villalva

unread,
Jan 21, 2012, 12:48:11 AM1/21/12
to django...@googlegroups.com
When you have an instance of an object and it has no id (that means, id is set to None) then it actually does not represent any object in your database, ergo, you can't delete it because it's not there.

Can you show us the portion of code where you are trying to delete it?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Regards,

Leandro Ostera,

Leandro Ostera Villalva

unread,
Jan 21, 2012, 12:40:13 PM1/21/12
to django...@googlegroups.com
No that I know. Can you show us some more code like how you defined the model and how do you come to this id=None issue?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Leandro Ostera Villalva

unread,
Jan 21, 2012, 4:41:04 PM1/21/12
to django...@googlegroups.com
Doesn't the Create method or the YourModel() constructor initialize the pk ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Leandro Ostera Villalva

unread,
Jan 21, 2012, 10:51:28 PM1/21/12
to django...@googlegroups.com
AFAIK when you try to access it ( quests[579] ) it gets loaded so performing .delete is possible.

My guess is that you are none-ifying the id somehow either by operating with it or by improper redefinition of the id field in your model.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages