0

Ya so i created this action button on a django web app and when i tried to use it got this error {"code":504,"message":"upstream request timeout"} so i do agree that its a big DB, although it would have become a big DB in future , so how can i get rid of this error

here is my code

class UserQuestionnaireAdmin(admin.ModelAdmin):
    readonly_fields = (
        "user",
        "questionnaire",
        "token",
        "completed_at",
        "created_at",
        "total_score"
    )

    inlines = [UserQuestionnaireAnswerInline]

    actions = ['export_as_csv']

    def export_as_csv(self, request, queryset):
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="questionnaire_results.csv"'
        writer = csv.writer(response)

        # Header row
        writer.writerow([
            "User ID",
            "User Name",
            "Questionnaire",
            "Question",
            "Answer",
            "Score",
            "Completed At",
        ])

        for uq in queryset:
            answers = uq.answers.all()
            # for those questionnaire which have populated rows
            if answers.exists():
                for answer in answers:
                    writer.writerow([
                        uq.user.id,
                        f"{uq.user.first_name} {uq.user.last_name}" if uq.user.first_name and uq.user.last_name else "",
                        uq.questionnaire.title if uq.questionnaire else "",
                        answer.question.text if answer.question else "",
                        answer.answer_option.text if answer.answer_option else "",
                        answer.answer_option.weight if answer.answer_option else "",
                        uq.completed_at if uq.completed_at else "",
                    ])
            else:
                # Write empty row for the questionnaire
                writer.writerow([
                    uq.user.id,
                    f"{uq.user.first_name} {uq.user.last_name}" if uq.user.first_name and uq.user.last_name else "",
                    uq.questionnaire.title if uq.questionnaire else "",
                    "", "", "",  # no question/answer/score
                    uq.completed_at if uq.completed_at else "",
                ])

        return response

    export_as_csv.short_description = "Export questionnaire results as CSV"
3
  • Share the corresponding models. Commented Sep 30 at 12:46
  • 1
    Maybe use django-import-export. This is made for this type of problem. Commented Sep 30 at 12:47
  • 1
    Just split the export results to smaller chunks or run it async (eg using django-celery) Commented Sep 30 at 17:11

0

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.