Why is the QuerySet not updating after save in django shell?

993 views Asked by At

So today , when i was learning Django shell interface for Database , i faced a very strange issue.

I couldn't get the updated the data even after doing the save method .

I searched about this issue, but in all those queries, they where missing save method.

Is this some django update issue or Am i Missing something?

>>> from hello.models import user
>>> user.objects.all()
<QuerySet []>
>>> user1 =  user("rins","[email protected]","9995584433","2000-01-01")
>>> user1.save
<bound method Model.save of <user: [email protected]>>
>>> user.objects.all()
<QuerySet []>

So this is the output. as you can see the user objects is still blank even after saving

And this is my model

class user(models.Model):
    name=models.CharField(max_length=30)
    email=models.CharField(max_length=30)
    phone=models.CharField(max_length=11)
    dob=models.DateField()
1

There are 1 answers

3
willeM_ Van Onsem On BEST ANSWER

There are two mistakes here:

  1. You did not call the method. You should call .save(), (so with parenthesis); and
  2. you should not use positional arguments, especially since the first one is normally the hidden id.

In the session, you thus can write this as:

user1 = user(name='rins',email='[email protected]',phone='9995584433',dob='2000-01-01')
user1.save()

Note: Models in Django are written in PerlCase, not snake_case, so you might want to rename the model from user to User.