Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7641011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:48:55+00:00 2026-05-31T08:48:55+00:00

How do you exclude the primary key from the JSON produced by Django’s dumpdata

  • 0

How do you exclude the primary key from the JSON produced by Django’s dumpdata when natural keys are enabled?

I’ve constructed a record that I’d like to “export” so others can use it as a template, by loading it into a separate databases with the same schema without conflicting with other records in the same model.

As I understand Django’s support for natural keys, this seems like what NKs were designed to do. My record has a unique name field, which is also used as the natural key.

So when I run:

from django.core import serializers
from myapp.models import MyModel
obj = MyModel.objects.get(id=123)
serializers.serialize('json', [obj], indent=4, use_natural_keys=True)

I would expect an output something like:

[
    {
        "model": "myapp.mymodel", 
        "fields": {
            "name": "foo", 
            "create_date": "2011-09-22 12:00:00", 
            "create_user": [
                "someusername"
            ]
        }
    }
]

which I could then load into another database, using loaddata, expecting it to be dynamically assigned a new primary key. Note, that my “create_user” field is a FK to Django’s auth.User model, which supports natural keys, and it output as its natural key instead of the integer primary key.

However, what’s generated is actually:

[
    {
        "pk": 123,
        "model": "myapp.mymodel", 
        "fields": {
            "name": "foo", 
            "create_date": "2011-09-22 12:00:00", 
            "create_user": [
                "someusername"
            ]
        }
    }
]

which will clearly conflict with and overwrite any existing record with primary key 123.

What’s the best way to fix this? I don’t want to retroactively change all the auto-generated primary key integer fields to whatever the equivalent natural keys are, since that would cause a performance hit as well as be labor intensive.

Edit: This seems to be a bug that was reported…2 years ago…and has largely been ignored…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:48:57+00:00Added an answer on May 31, 2026 at 8:48 am

    The problem with json is that you can’t omit the pk field since it will be required upon loading of the fixture data again. If not existing, json will fail with

    $ python manage.py loaddata some_data.json
    [...]
    File ".../django/core/serializers/python.py", line 85, in Deserializer
    data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])}
    KeyError: 'pk'
    

    As pointed out in the answer to this question, you can use yaml or xml if you really want to omit the pk attribute OR just replace the primary key value with null.

    import re
    from django.core import serializers
    
    some_objects = MyClass.objects.all()
    s = serializers.serialize('json', some_objects, use_natural_keys=True)
    # Replace id values with null - adjust the regex to your needs
    s = re.sub('"pk": [0-9]{1,19}', '"pk": null', s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that stores photo information with id as Primary key: id(PK),
For some reason, the exclude in my forms isn't working and the primary key
I want to exclude properties from bean to json using json-lib at runtime. How
How do I exclude entire files from coverage.py reports? According to the documentation you
Is it possible to exclude specific files or folders from Build Deployment Package function
How do I exclude specific .dlls from Visual Studio 2003 C++ .NET? During release
I want to exclude the following characters from my string: \-- ' < >
How can I exclude null values from a count on an array? since count
How best would I exclude results from #other_section, given a structure like this? <form>
If you have the select statement below where the PK is the primary key:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.