Opened 5 years ago

Closed 5 years ago

#13930 closed (duplicate)

Serialize PKs using the natural key

Reported by: jbochi Owned by: nobody
Component: Core (Serialization) Version:
Severity: Keywords: pk, natural key, serialization
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Django uses natural keys only to serialize FKs and M2M fields. Serializing the PK with the natural key can also be useful.

Assuming the following object:

   {
      "pk": 1, 
      "model": "auth.permission", 
      "fields": {
         "codename": "add_logentry", 
         "name": "Can add log entry", 
         "content_type": [
            "admin", 
            "logentry"
         ]
      }
   }, ...

If we use the command loaddata to import this object to the database, an error will be raised if the permission add_logentry already exist with a different id or if another permission has the id 1. That problem can be avoided by using the natural key instead of the pk id number:

   {
      "pk": [
         "add_logentry", 
         "admin", 
         "logentry"
      ], 
      "model": "auth.permission", 
      "fields": {
         "codename": "add_logentry", 
         "name": "Can add log entry", 
         "content_type": [
            "admin", 
            "logentry"
         ]
      }
   }, ...

Attachments (1)

pk_natural_key.diff (1.7 KB) - added by jbochi 5 years ago.

Download all attachments as: .zip

Change History (2)

Changed 5 years ago by jbochi

comment:1 Changed 5 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Dupe of #13252

Note: See TracTickets for help on using tickets.
Back to Top