0

When I read the source code, I find the function use pk to as keyword to select data:

def detail(request, album_id):
    try:
        album = Album.objects.filter(pk=album_id)
    except Album.DoesNotExist:
        raise Http404("Album does not exist")

    context = {
        "album":album,
    }

    return render(request, "music/detail.html", context)

I am used to use id:

album = Album.objects.filter(id=album_id)

So, is there somewhere different between them?

maer
  • 156
  • 3
  • 15

1 Answers1

2

In django id field is by default the pk hence you can use both.

Difference:

But you can manually set pk and then it may not be id field

Astik Anand
  • 12,757
  • 9
  • 41
  • 51