0

I'm working on Django blog and I want to export posts and I made it work but I have a problem when exporting text because I used Quill - rich text editor and Django Import Export

body = QuillField()

And when I export posts in excel, I got this <django_quill.fields.FieldQuill object at 0x7f0746389840>.

excel is looking like this, image

This is resources.py

from import_export import resources
from .models import Post

class PostResource(resources.ModelResource):
    class Meta:
        model = Post

This is views.py

def export_data(request):
    if request.method == 'POST':
        # Get selected option from form
        file_format = request.POST['file-format']
        post_resource = PostResource()
        dataset = post_resource.export()
        if file_format == 'CSV':
            response = HttpResponse(dataset.csv, content_type='text/csv')
            response['Content-Disposition'] = 'attachment; filename="exported_data.csv"'
            return response        
        elif file_format == 'JSON':
            response = HttpResponse(dataset.json, content_type='application/json')
            response['Content-Disposition'] = 'attachment; filename="exported_data.json"'
            return response
        elif file_format == 'XLS (Excel)':
            response = HttpResponse(dataset.xls, content_type='application/vnd.ms-excel')
            response['Content-Disposition'] = 'attachment; filename="exported_data.xls"'
            return response   

    return render(request, 'dashboard/export.html')

Any idea how to get the text in this body column?

Thanks in advance!

4
  • It seems you are exporting the python object and not the text inside Commented Jan 15, 2023 at 18:12
  • How can I export text inside? Commented Jan 15, 2023 at 18:28
  • You probably need to define more in the PostResource class Commented Jan 15, 2023 at 19:56
  • See Import Export Doc django-import-export.readthedocs.io/en/stable/… Commented Jan 15, 2023 at 19:58

3 Answers 3

1

You have to get the content from the quill object Post.body.html or Post.body.html

Sign up to request clarification or add additional context in comments.

1 Comment

this works if I use tags in HTML, but how do I use in my case when I'm using export-import and exporting all posts?
1

Try something like this

from import_export import resources
from import_export.fields import Field
from .models import Post

class PostResource(resources.ModelResource):
    body = Field() 
 
    class Meta:
        model = Post
    
    def dehydrate_body(self,post):
        body_content = post.body.html
    
        return body_content

        

See docs : https://django-import-export.readthedocs.io/en/stable/getting_started.html#advanced-data-manipulation-on-export

and Django Quill Editor Display Saved Field

Comments

0

Try using Post.body.html|safe

That usually works for me

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.