There is a certain model in django.

When I tested it, I added and deleted records in the admin panel, only one record was left there. In the view I need to get this record, I thought I would get it through get(pk=1) , but as it turned out she had pk equal to 6. That is, those records I entered for the test were not completely removed?

Can I somehow update the state of the table so that the element has pk = 1 ?

PS Tried through objects.all()[:1] , also does not work ...

  • one
    MyModel.objects.all().first() - for getting the first object - Andrey
  • 2
    Primary key is automatically assigned to models by default - this is not the ordinal number of the entry in the table, but its unique identifier and it does not change by itself. You can get your entry through get (pk = 6) or object.all () [0] and change pk forcibly - Alexander
  • Clear. I just didn’t understand the essence correctly :) - Antoxer

1 answer 1

All essence of identifiers in their immutability. No need to try to reset them to the initial value. And to get the latest entry, you can use SomeModel.objects.last() .