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 6353863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:29:05+00:00 2026-05-24T22:29:05+00:00

I have a djano web app which stores the data about some entries to

  • 0

I have a djano web app which stores the data about some entries to postgres db.To copy the data in db to json files ,I generally use the python manage.py shell and use the serialization api as mentioned i django tutorial.

>>>python manage.py shell
...
In[8]:from myapp.models import MyFirstModel
In[9]:data = serializers.serialize("xml", MyFirstModel.objects.all())
In[10]:print data

I copy this output to some text file and save it as json.

I thought of writing a script to do this and tried

datacopy.py
……..

...
filename = os.path.join(dirpath,basefilename+".json")

def write_data_to_file():
    from django.core import serializers
    XMLSerializer = serializers.get_serializer("json")
    xml_serializer = XMLSerializer()
    out = open(filename,"a")
    from django.contrib.auth.models import User
    from myapp.models import MyFirstModel
    from myapp.models import MyNextModel
    xml_serializer.serialize(User.objects.all(), stream=out)
    xml_serializer.serialize(MyFirstModel.objects.all(), stream=out)
    xml_serializer.serialize(MyNextModel.objects.all(), stream=out)

if __name__ == '__main__':
    write_data_to_file()

From bash shell,I tried

>>python datacopy.py

But, this writes only the User model’s data and fails to copy the models which I create in my app.

The error message I get

Traceback (most recent call last):
  File "datacopy.py", line 29, in <module>
    write_data_to_file()
  File "datacopy.py", line 23, in write_data_to_file
    xml_serializer.serialize(MyFirstModel.objects.all(), stream=out)
  File "/home/me/Django-1.1.1/django/core/serializers/base.py", line 38, in serialize
    for obj in queryset:
  File "/home/me/Django-1.1.1/django/db/models/query.py", line 106, in _result_iter
    self._fill_cache()
  File "/home/me/Django-1.1.1/django/db/models/query.py", line 692, in _fill_cache
    self._result_cache.append(self._iter.next())
  File "/home/me/Django-1.1.1/django/db/models/query.py", line 238, in iterator
    for row in self.query.results_iter():
  File "/home/me/Django-1.1.1/django/db/models/sql/query.py", line 287, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/me/Django-1.1.1/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params)
  File "/home/me/Django-1.1.1/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "myapp_myfirstmodel" does not exist

I am able to copy data of all three models when I use the python manage.py shell.Why does this error happen when I run the script from bash?I have the modules of myapp in PYTHONPATH

  • 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-24T22:29:06+00:00Added an answer on May 24, 2026 at 10:29 pm

    Have you looked in to using ./manage.py dumpdata ? You can specify the serialization format.

    For your script to work have you set DJANGO_SETTINGS to the correct settings.py ?

    Secondly is there a reason that you are doing your imports inside the function, probably better to move them to the head of the file:

    from django.core import serializers
    from django.contrib.auth.models import User
    from myapp.models import MyFirstModel
    from myapp.models import MyNextModel
    
    def write_data_to_file():
        XMLSerializer = serializers.get_serializer("json")
        xml_serializer = XMLSerializer()
        out = open(filename,"a")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web app which tries to determine when people are attending events.
I have an application solution which is made up of a web app written
I have a reviews/ratings web application, a la Digg. My django app content has
I have recently started experimenting with Django for some web applications in my spare
we have create a web application on top of google app engine and python.
I am working on a web-app using Django 1.3 and Python2.6. I have to
I have an existing django web app that is in use. I have to
I have recently switched to Django for a web app I'm developing and I
I am developing a Django app being a Web frontend to some Oracle database
I had the following idea: Say we have a webapp written using django which

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.