I'm in the process of moving a django (v1.1) project from mysql to postgresql (fun!) and currently I am transferring all of the data. I attempted to use the manage.py dumpdata option, but the server we're using is rather old and would take a really long time (it seems to want to load everything into memory). I came up with a small script to process each app and then each model under it, the simplified version is shown below:
def dumpJson(model, outfile):
try:
data = serializers.serialize("json", model.objects.all(),indent=4)
except model.DoesNotExist:
print model.__name__ , "Does not Exist (not Object)"
except ObjectDoesNotExist:
print model.__name__ , "Issue with the data, not sure what...."
data = serializers.serialize("json", model.objects.all(),indent=4)
data = data.decode("unicode_escape").encode("utf8")
out = open(outfile, "w")
out.write(data)
out.close()
def main():
for app in get_apps():
for model in get_models(app):
print " -- Dumping:", model.__name__
outfile = "json_dumps/" + model.__name__ + ".json"
dumpJson(model, outfile)
However, if I remove the try catch statements, I get the following error:\
-- Dumping: Institution
Traceback (most recent call last):
...(nasty stacktrace)....
unity.trip.models.DoesNotExist: USNWRData matching query does not exist.
My model is defined like so:
class USNWRData(models.Model):
rank = models.PositiveIntegerField(blank=True, null=False)
name = models.CharField(max_length=255, blank=True, null=False)
state = models.CharField(max_length=10, blank=True, null=False)
public = models.BooleanField(blank=True, null=False)
type = models.ForeignKey(USNWRType)
class Institution(models.Model):
name = models.CharField(max_length=200)
parent_institution = models.ForeignKey('self', blank=True, null=True)
location = models.ForeignKey(Location, blank=True, null=True,related_name='old_location')
type = models.ForeignKey(InstitutionType, blank=True, null=False )
usnwr = models.ForeignKey(USNWRData, blank=True, null=True)
locationnrm = models.ForeignKey(LocationNrm, blank=False, null=True)
As I understand it, the cause of this error is from some sort of data mismatch or missing foreign keys. Given the above model, what seems to be the issue? I'm having a bit of trouble with it and the original author is long gone. Any help would be much appreciated!
EDIT: The full stack trade is:
-- Dumping: Institution
Traceback (most recent call last):
File "custom_dump.py", line 38, in <module>
main()
File "custom_dump.py", line 34, in main
dumpJson(model, outfile)
File "custom_dump.py", line 16, in dumpJson
data = serializers.serialize("json", model.objects.all(),indent=4)
File "/usr/local/lib64/python2.5/site-packages/django/core/serializers/__init__.py", line 91, in serialize
s.serialize(queryset, **options)
File "/usr/local/lib64/python2.5/site-packages/django/core/serializers/base.py", line 48, in serialize
self.handle_fk_field(obj, field)
File "/usr/local/lib64/python2.5/site-packages/django/core/serializers/python.py", line 48, in handle_fk_field
related = getattr(obj, field.name)
File "/usr/local/lib64/python2.5/site-packages/django/db/models/fields/related.py", line 315, in __get__
rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
File "/usr/local/lib64/python2.5/site-packages/django/db/models/query.py", line 349, in get % self.model._meta.object_name)
unity.trip.models.DoesNotExist: USNWRData matching query does not exist.
I've found Django fixtures really useless for any other purposes than a small dataset (say, 100 records of Foo OptionList items) or test fixtures. I would approach your problem by exporting the MySQL tables to CSV.
Additionally, you might take a look at this:
http://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL#MySQL