Code

Opened 4 years ago

Closed 4 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 4 years ago.

Download all attachments as: .zip

Change History (2)

Changed 4 years ago by jbochi

comment:1 Changed 4 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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.