5

I have a django app that renders querysets into tables.

I need to perform a few modifications of the underlying querysets and it would be the most convenient to convert the querysets into dataframes, do some magic there and then convert the resulting dataframe back into a queryset so that I can feed it into the existing render.

Converting a queryset into a dataframe seems straightforward, but I ccan't find a way to convert that back.

tables.py

class StuffTable(tables.Table):
    class Meta:
        model = Stuff
        template_name = 'django_tables2/semantic.html'


views.py

import django_pandas.io as dpd
from django_tables2.tables import Table
from .models import Stuff
from .tables import StuffTable

def index(request):

    context = dict()
    template = 'gui/index.html'

    stuff = Stuff.objects.all()

    # do pandas stuff with the data
    df = dpd.read_frame(stuff)
    df = ...
    modified_stuff = convert_df_back_to_qs(df) # how to do this?

    table = StuffTable(modified_stuff)
    context['table'] = table

return render(request, template, context)

How can I do this?

EDIT: The reason why I want to convert back is that the html files look like

{% load render_table from django_tables2 %}
<body>
    {% render_table table %}
</body>

So, if I can convert the data back into a queryset I would not need to touch any of the html files.

chrise
  • 4,039
  • 3
  • 39
  • 74

3 Answers3

3

Really, that does not seem to be trivial, since QuerySet sorta expects there to be a relational database beneath the hood. You could implement an adapter QuerySet-like class that would have the same methods as a QuerySet, but implement its functionality as pandas calls.

You can also look at this similar question django - convert a list back to a queryset; again, there are no easy answers.

The main question is: do you really need the QuerySet functionality?

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
1

You can do something like this...

import pandas, os

excel = pandas.read_excel(os.path.join(BASE_DIR, 'path_to_xlsx')) 

cols = [i for i in excel]
queryset = [dict(zip(cols, i)) for i in excel.values]

Then you can access this like some other queryset from your database

0

Actually you can do it by looping over rows on df and creating a model objects from this rows here is the example : queryset = [NameOfYourModel(**vals) for vals in your_df.to_dict('records')]

asmatrk
  • 237
  • 2
  • 9