how to use JOIN query or use connections.cursor() in Django Datatables View
Issue #14
closed
I am using django-datatables-view library. I am able to get and show data in datatables using MODAL But I need to execute joining query and show data in datatables. Please check my code.
from django_datatables_view.base_datatable_view import BaseDatatableView from django.db import models from webapi.models import User
from django.db import connections
class BuyersTable(BaseDatatableView):
columns = ['user_name','department_name']
order_columns = ['user_name','department_name']
search_columns = ['user_name','department_name']
def get_initial_queryset(self):
#return User.objects.all()#=> this is working using MODAL but it doesn't work using executing query in CURSOR
cursor = connections['my_database'].cursor()
return cursor.execute("select * from user, department where user.id = department.user_id")
def prepare_results(self, qs):
# prepare list with output column data
# queryset is already paginated here
json_data = []
for item in qs:
json_data.append([
item.user_name,
item.department_name
])
return json_data
Only user_name datatables works fine(did without using cursor.execute and without JOIN) but user_name with department_name datatables doesn't work. I need to JOIN TABLES and show data.
I execute this code and it showing error. 'pyodbc.Cursor' object has no attribute 'count'
Please help me to solve this problem. I have searched a lot in google but I get nothing.
Comments (2)
-
repo owner -
repo owner - changed status to closed
- Log in to comment
Your problem is not related to DjangoDatatablesView. It's more a problem with understanding core Django concepts. Anyway, using cursor is a bad idea. I don't know your models but it seems that you should be able to do something like:
where item is instance of User model and department is defined as ForeignKey or OneToOne field. Have a look at: https://docs.djangoproject.com/en/1.8/topics/db/queries/#related-objects to get to know how to work with related objects in Django
For further questions it might be better to ask at StackOverflow