0

How do you delete a row in a Django database at the interpreter level. I'm troubleshooting a database and I would like the ability to target specific rows in a Django sqlite3 database.

Currently I can create entries in the database as follows:

>> Item.objects.create(text='Item A')

Which I can view in the database using:

>> for p in Item.objects.raw('SELECT * FROM appName_model'):
...    print(p.text)
...

And I correctly get the database contents (their text fields).

Having trouble with targeting a specific row by index, text_field or by some other attribute. I am troubleshooting directly at the Python interpreter.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jeeves
  • 424
  • 1
  • 7
  • 25

1 Answers1

5
Item.objects.get(text='Item A').delete()

EDIT:

By the way, Django has more methods for interacting with models. For example, looping over the Item objects:

for item in Item.objects.all():
    print item.name

Source: https://docs.djangoproject.com/en/1.9/topics/db/queries/

justdavey
  • 226
  • 1
  • 4
  • This works, thanks. Though I must add, I got an error of (django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet) which is solved via : [[ http://stackoverflow.com/questions/25537905/django-1-7-throws-django-core-exceptions-appregistrynotready-models-arent-load ]], by adding BEFORE the delete in the command-line python interpreter : >> from django.core.wsgi import get_wsgi_application >> application = get_wsgi_application() – Jeeves May 10 '16 at 20:43
  • 1
    Great! And... You can use `python manage.py shell` to do that for you. – justdavey May 10 '16 at 20:53
  • How would I delete the first element, second element, etc? – Jeeves May 10 '16 at 21:52
  • `Item.objects.all()[0].delete()`, `Item.objects.all()[1].delete()`, and so on. – justdavey May 11 '16 at 22:11