Here's something I wrote to export database contents from a django application into the Django database API format.

It's certainly not going to do all the work for you - in particular you'll need to reorder things to make sure things that Django wants to be objects (i.e. Foreign Keys) are instantiated before the related object gets made.

Hope this helps someone somewhere.

PYTHON:
  1. #!/usr/bin/env python
  2. """
  3. Script to export data from a Django database and generate
  4. the appropriate django DB-API commands
  5. Make sure you run it from the same directory as your settings.py
  6. NOTE: it does NOT handle relationships yet, so, you'll need to take the output
  7. and hand edit it to make sure that anything that Django needs as an object,
  8. gets instantiated first
  9. (c) Simon Greenhill / dev@simon.net.nz / http://simon.net.nz
  10. Version: 0.00001
  11. """
  12.  
  13. debug = False # just a debug flag to spit out what we're doing.
  14.  
  15. import sys
  16.  
  17. from django.core.management import setup_environ
  18.  
  19. try:
  20. import settings
  21. except ImportError:
  22. print "You don't appear to have a settings file in this directory!"
  23. print "Please run this from inside a project directory"
  24. sys.exit()
  25.  
  26. setup_environ(settings)
  27.  
  28. from django.db import models
  29.  
  30. # do model imports
  31. print """
  32. ###############################################################
  33. #                                                             #
  34. #   Model imports                                             #
  35. #                                                             #
  36. ###############################################################
  37. """
  38.  
  39. for app in models.get_apps():
  40. for model in app.models.get_models():
  41. print "from %s import %s" % ( app.__name__, model._meta.object_name )
  42.  
  43. # what apps we've done
  44. seen = []
  45. for app in models.get_apps():
  46. for model in app.models.get_models():
  47. if model not in seen:
  48. print
  49. print "# %s.%s " % (app.__name__, model._meta.object_name)
  50. print
  51.  
  52. if debug:
  53. print "#\t", model
  54.  
  55. for row in model.objects.iterator():
  56.  
  57. if debug:
  58. print "#\t\t",row
  59.  
  60. attrs = []
  61. for col in row._meta.fields:
  62. key = col.name
  63. value = getattr(row, key)
  64. if col.get_internal_type() == 'ForeignKey':
  65. ###print key, 'bork bork bork'
  66. pass
  67.  
  68. # if it's numeric, we don't need to quote it.
  69. if isinstance(value,int) or isinstance(value,float):
  70. pass
  71. # if it's a string...
  72. elif isinstance(value,str):
  73. # ... and it's got a quote mark or new line in it, then triple-quote it.
  74. if value.find('"')> 0 or value.find("\n")> 0:
  75. value = '"""%s"""' % value
  76. # ... otherwise single quote it.
  77. else:
  78. value = '"%s"' % value
  79. # don't know what it is, but single quote it
  80. else:
  81. value = '"%s"' % value
  82.  
  83. attrs.append( '%s=%s' % ( key, value ) )
  84.  
  85. if debug:
  86. print "#\t\t\t", key, getattr(row, key)
  87.  
  88. print """%s(%s).save()""" % ( model._meta.object_name, ', '.join(attrs) )
  89.  
  90. seen.append(model)

Update 19/01/2007: limodou has written a far superior export solution here.

--Simon