maer
maer

Reputation: 156

Is there somewhere different between `pk` and `id`?

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?

Upvotes: 0

Views: 104

Answers (1)

Astik Anand
Astik Anand

Reputation: 13047

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

Upvotes: 2

Related Questions