Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Django ORM: Accessing a Single Project

To get started, go into the Django shell and bring up models.py so you can have an overview of what’s in that file. First, you need to import:

>>>
>>> from projects.models import Project

Now, you can interact with it and revisit what you did to get all of the models:

>>>
>>> projects = Project.objects.all()

Comments & Discussion

reblark on Nov. 12, 2019

In a previous exchange with you I mentioned the possibility of using python manage.py flush to take out the entries in the database. There were three entries that I have made. I went ahead and did that so I had a db with no rows. Using the shell cl funcxtion, I added three more. The first three no longer appear, but when I use “id” or “pk”, I have to use id=4 (pk=4) and id=5 (pk=6). That tells me that the first three entries are not really gone. Where are they?

Martin Breuss RP Team on March 8, 2020

Hi @reblark! Seems I missed this comment of yours, sorry about that, and nice discovery!

What’s Happening Here?

What you’ve encountered is standard behavior for a database. The ID of an entry should uniquely identify a given entry, and there’s no reason for your database to assume that you’d want a new entry to take on the ID of an old, deleted entry. Django’s flush follows that convention.

So, to answer your question: The entries are really gone, but their IDs are now out of the game as far as your database is concerned.

How To Reset IDs

If you really want to reset the IDs, the easiest way is to delete your database file and recreate it (when using SQLite), or write some SQL code to drop the table and recreate it (when using e.g. PostgreSQL).

You can read more about all this in this StackOverflow thread and this blog post.

Become a Member to join the conversation.