301👍
As of version 1.1 and greater, the Django dumpdata
management command allows you to dump data from individual tables:
./manage.py dumpdata myapp1 myapp2.my_model
You can also separate multiple apps and models on the command line. Here’s the canonical definition:
django-admin dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]
118👍
As noted, you can’t do this through a manage.py command in Django 1.0. However you could use a script to export the JSON file, and load it using loaddata
:
from django.core import serializers
from myproject.myapp import models
data = serializers.serialize("json", models.MyModel.objects.all())
out = open("mymodel.json", "w")
out.write(data)
out.close()
- [Django]-What is the purpose of adding to INSTALLED_APPS in Django?
- [Django]-Cross domain at axios
- [Django]-IOS app with Django
22👍
A specific model to a specific file:
python manage.py dumpdata app_label.ModelName > specific_file.json
and to load it to another app: first move or copy the file to the app where you want process it, then:
python manage.py loaddata specific_file.json
- [Django]-How can I filter a Django query with a list of values?
- [Django]-Google Static Maps URL length limit
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
12👍
Take all data into json format from django model.
Syntax:
python manage.py dumpdata app_name.model_name
For example dumping data from group_permission model which reside in default auth app in django.
python manage.py dumpdata auth.group_permission
For output take a look on console.
- [Django]-How can I see the raw SQL queries Django is running?
- [Django]-Django Sitemaps and "normal" views
- [Django]-Having Django serve downloadable files
6👍
I think you had the solution in your question. You can dump an individual model like this:
./manage.py dumpdata myapp.my_model
- [Django]-How do you use the django-filter package with a list of parameters?
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?
- [Django]-Checking for empty queryset in Django
5👍
For success I had to say it twice, and specify the model two times, like:
./manage.py dumpdata myapp2.my_model myapp2.my_model
If I only said
./manage.py dumpdata myapp2 myapp2.my_model
I got flooded with all the models in myapp2, despite the fact that I specified my_model.
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-Django-way for building a "News Feed" / "Status update" / "Activity Stream"
- [Django]-Django Admin – Disable the 'Add' action for a specific model
3👍
For newbies in Django and Python like me, this might be useful:
if you want to only dump a single row (of, obviously, a single table) and you have for example the app "merchant" and the model is also named "Merchant", and you usually import it using a fully qualified name like this: merchant.models.Merchant
; do not even try to use this name. The syntax is as follows:
# only dumps the merchant with id = 123, pretty printed
python manage.py dumpdata merchant.Merchant --pks 123 --indent 2
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
- [Django]-Django composite unique on multiple model fields
- [Django]-Gunicorn Connection in Use: ('0.0.0.0', 5000)
2👍
To write it on specific file:
python manage.py dumpdata app_label.ModelName app_label.ModelName2 > fixtures/specic.json
- [Django]-Parsing unicode input using python json.loads
- [Django]-What is the difference render() and redirect() in Django?
- [Django]-Using the reserved word "class" as field name in Django and Django REST Framework
2👍
If you want to dump only the specified objects of a model, you can use the –pks optional argument for the dumpdata command.
–pks PRIMARY_KEYS Outputs only the objects specified by a comma separated list of primary keys. This is only available when dumping
one model. By default, all the records of the model are output.
- [Django]-(13: Permission denied) while connecting to upstream:[nginx]
- [Django]-Where to put business logic in django
- [Django]-How can I create a deep clone of a DB object in Django?
1👍
As a workaround you could make another app and copy the model but point it to the existing table with the db_table meta option. Then you could just dump the models you copied into the new app. You existing app wouldn’t be affected.
- [Django]-How to get the name of current app within a template?
- [Django]-What's the difference between CharField and TextField in Django?
- [Django]-Django Admin app or roll my own?
1👍
I’ve created a management command the generate a fixture on a per model basis.
Fixtures can be generated by running:
./manage generate_fixtures app.model.MyModel --file=dump/MyModel.json
code at: https://gist.github.com/2394883
- [Django]-Add rich text format functionality to django TextField
- [Django]-Django – How to rename a model field using South?
- [Django]-How can I build multiple submit buttons django form?
1👍
I think the accepted answer is old and needs a refresher in 2023 with the detailed approach.
I had the similar problem where I had to create fixtures for specific models from different apps having specific id’s. Moreover I had to create just one fixture for all models. so that we don’t have to load individual fixtures rather just one fixture for all models.
I did all of this by creating a custom command that calls the dumpdata
internally for each specific model with the specific ids like the following:
products/models.py
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits = 5, decimal_places = 2)
reviews/models.py
class Review(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
rating = models.PositiveIntegerField(default=0)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
def natural_key(self):
return self.title, self.timestamp
class Response(models.Model):
text = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
And following is the command that creates one fixtures for specific models from different apps with specific id’s:
generate_fixture.py
from django.core.management import BaseCommand, call_command
from io import StringIO
import json
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('id', type=int, help='Primary Key Of Product')
def handle(self, *args, **options):
id = options.get('id')
if product := Product.objects.filter(id=id).first():
product_fixture = self.get_fixture('products.Product', str(id))
review_ids = list(
Review.objects.filter(product__id=id).values_list('id',flat=True)
)
review_ids = ','.join([str(review_id) for review_id in review_ids])
review_fixture = self.get_fixture('reviews.Review', review_ids)
output = [json.loads(product_fixture)]
output.extend(json.loads(review_fixture))
with open('model_fixtures.json', "w") as file:
json.dump(output, file)
else:
print(f"Product with id {id} does not exists!")
def get_fixture(self, label, pks):
args = ['dumpdata', '--natural-foreign', '--natural-primary', label]
with StringIO() as buffer:
call_command(*args, pks=pks, stdout=buffer)
data = buffer.getvalue()
return data
Now you can run the command with the id like this:
python manage.py generate_fixture 50
And fixture of the models will be generated in just one fixture file which can be loaded like this:
python manage.py loaddata model_fixtures.json
Note: The code is not tested.
- [Django]-Http POST drops port in URL
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
- [Django]-Why doesn't django's model.save() call full_clean()?
0👍
For DUMPING data out of the specific model for specific APP.
If we take EXAMPLE where we have in a MAIN PROJECT (We go call it project) and in this MAIN PROJECT we have two (02) others applications (We go call each app like this APP1 and APP2) we can do like that
- python manage.py dumpdata APP1.name_of_model >APP1/fixtures/name_of_model .json
- python manage.py dumpdata APP2.name_of_model >APP2/fixtures/name_of_model .json
- [Django]-How does django handle multiple memcached servers?
- [Django]-How can I handle Exceptions raised by dango-social-auth?
- [Django]-Django queries – id vs pk