Saving Datatable Data to DB and Retreiving Datat from DB on Page Reload

Saving Datatable Data to DB and Retreiving Datat from DB on Page Reload

movingon953movingon953 Posts: 2Questions: 1Answers: 0

I am using Django, Datatables and Postgres.
Problem: **I have 2 Datatables
**Table1.
Table from where I can select row and add to the 2nd Datatable existing on same page. (This Functionality I have got working)
Table2. By default I should be able to retreive data from DB on Page refresh and then after doing any changes be able to save to DB. (Not Able to get it to work)
a# I tried to use the "<model.name>.save()" in the Model, thinking the Model will manage the queries in backend. But do not see any interaction/error with the DB.
b# I created sample data in DB to try retreive it to Datatable2, but again do not seem to get any data in the views.py
Can someone guide me with what I need to consider for this to happen.

Example Table is:

Sample views.py

    class Info_PageView(TemplateView):
          def get(self, request):
               tab_data = Model_1.objects.all()
               print(tab_data)
               return render(request, 'info.html', context = { 'tab_data': tab_data }  )

models.py
class Model_1(models.Model):
column_1 = models.CharField('name', max_length=50, null=False, blank=False, primary_key=True)
column_2 = models.CharField('age', max_length=15, null=False, blank=False,)
column_3 = models.CharField('city', max_length=15, null=False, blank=False,)
history = HistoricalRecords()

    class Meta:
        ordering = ['column_1']        

    def __str__(self):
        return self.column_1

    def save(self):
        save_page = Model_1.objects.all()
        print("In Save")   //None of these get displayed so think there is some code issue
        print(save_page)
        Model_.save()

    @property
    def _history_user(self):
        return self.changed_by

Answers

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    Table2. By default I should be able to retreive data from DB on Page refresh and then after doing any changes be able to save to DB. (Not Able to get it to work)

    Are you using Datatables Editor or your own code for editing the table?

    You will first need to use an ajax request in the client to send the updated data to the server. Do you have this?

    Please post your Datatabalse config and info about how you are editing the data.

    Kevin

  • movingon953movingon953 Posts: 2Questions: 1Answers: 0

    Hi Kevin,

    Thanks for talking a look at the query and the reply.
    I new to web development so was not sure what was needed to achieve the read/write from DB. For some reason I felt the datatable models will handle this.

    From what you mentioned, I will work on the ajax query to generate the read / write operation. With regards to editing the table I do not have Datatables Editor Package setup so will be doing this via own code. Will post with the update soon.

    Thanks!!

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    When I first started with Datatables I was new to web development too. I opted to purchase the Editor to save a lot of grief. Glad I did. Its well worth the small price of $119.

    Kevin

This discussion has been closed.