Get name of primary field of Django model

Solution 1:

You will also have an attribute "name" on the pk-attribute. This seems to hold the name of the Field.

CustomPK._meta.pk.name

In my case I get the value "id" as result (like it should). :-)

Solution 2:

Field objects have a primary_key attribute

for field in CustomPK._meta.fields:
  print field.primary_key
  print field.name


# True
# primary
# False
# payload