开发者

psycopg2.ProgrammingError, running script to serialize data of django app from postgres

开发者 https://www.devze.com 2023-03-31 03:15 出处:网络
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 mention

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)
    xm开发者_高级运维l_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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号