.save is not, well, saving.

I am following a tutorial, and the .save function in the python shell is not working (it was working fine a couple of hours ago!)

Here is /myproject/articles/models.py:

from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField()
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)

Here is what I called in the terminal:

Seems pretty cut and dry that I should be getting an id back, but nope. Nothing appears if I do Articles.objects.all() either.

Thanks for the help!

.save is a method. So call it like a.save()

Yep! Somehow, despite checking five times, I missed that.

Python question in general, do all method calls require parenthesis?

Why does .id not?

All class methods need to be called with parenthesis to run.

.id is a class attribute and can not be called, it only stores a value.

Further, there are class properties. You can read more on that in the Python documentation.

1 Like

Another hint: the output from Python tells you here that nothing happened. When you wrote .save, the output came back: <bound method ...>. This is Python’s representation of what .save is. If you call .save(), it returns None, so you won’t see any output before the next prompt, like:

>>> a.save()
>>>
1 Like