>
> this might come of use, it exports any query set to csv. I use it a lot:
>
>
def queryset_export_csv(qs):
    import csv
    #from django.db.models.loading import get_model

    response = HttpResponse(mimetype='text/csv')
    # force download.
    response['Content-Disposition'] = 
'attachment;filename="service_export.csv"'

    # the csv writer
    writer = csv.writer(response)

    qs_model = qs.model

    headers = []
    for field in qs_model._meta.fields:
        headers.append(field.name)
    writer.writerow(headers)

    for obj in qs:
        row = []
        for field in headers:
            val = getattr(obj, field)
            if callable(val):
                val = val()
            if type(val) == unicode:
                val = val.encode("utf-8")
            row.append(val)
        writer.writerow(row)

    return response 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to