<reponame>jjhenkel/dockerizeme<gh_stars>10-100
import csv

from tools import DynamicCommand # see https://gist.github.com/2724472


class Command(DynamicCommand):
    """
    Easily export a model's objects in csv format.  In this example the csv can be generated by executing:

    ./manage.py export your_model
    """

    def your_model(self):
        from your.app.models import YourModel
        meta = {
            'file': '/tmp/your_model.csv',
            'class': YourModel,
            'fields': ('title', 'description') # models fields you want to include 
        }
        self._write_csv(meta)

    def _write_csv(self, meta):
        """
        :param meta: (dict) keys should be 'file' (string: absolute path), 'class' the Python class
        object, 'fields' a list or tuple of field model field names (strings)
        """
        f = open(meta['file'], 'w+')
        writer = csv.writer(f, encoding='utf-8')
        writer.writerow( meta['fields'] )
        for obj in meta['class'].objects.all():
            row = [unicode(getattr(obj, field)) for field in meta['fields']]
            writer.writerow(row)
        f.close()
        print 'Data written to %s' % meta['file']
import csv

from tools import DynamicCommand # see https://gist.github.com/2724472


class Command(DynamicCommand):
    """
    Easily export a model's objects in csv format.  In this example the csv can be generated by executing:

    ./manage.py export your_model
    """

    def your_model(self):
        from your.app.models import YourModel
        meta = {
            'file': '/tmp/your_model.csv',
            'class': YourModel,
            'fields': ('title', 'description') # models fields you want to include 
        }
        self._write_csv(meta)

    def _write_csv(self, meta):
        """
        :param meta: (dict) keys should be 'file' (string: absolute path), 'class' the Python class
        object, 'fields' a list or tuple of field model field names (strings)
        """
        f = open(meta['file'], 'w+')
        writer = csv.writer(f, encoding='utf-8')
        writer.writerow( meta['fields'] )
        for obj in meta['class'].objects.all():
            row = [unicode(getattr(obj, field)) for field in meta['fields']]
            writer.writerow(row)
        f.close()
        print 'Data written to %s' % meta['file']
# see https://gist.github.com/2724472 Easily export a model's objects in csv format.  In this example the csv can be generated by executing:      ./manage.py export your_model # models fields you want to include :param meta: (dict) keys should be 'file' (string: absolute path), 'class' the Python class         object, 'fields' a list or tuple of field model field names (strings)