1

I am using django 1.4 together with jqgrid from django_gems package The following code is trying to bring the virtual column onto the grid by concatenating first and last name.

However it fails with

Cannot resolve keyword client__get_fullname into field.

Is there any acceptable way to achieve this?

 class Car(models.Model):
     number = models.CharField(max_length = 50)
 client = models.ForeignKey('Client')

 class Client(models.Model):
     first_name = models.CharField(max_length = 50)
 last_name = models.CharField(max_length = 50)
 def get_fullname(self):
     return '%s %s' % (self.first_name, self.last_name)

 from django_gems.jqgrid
 import JqGrid
 class CarGrid(JqGrid):
     queryset = Car.objects.all()
 fields = ['number', 'client__get_fullname']

 jqgrid config = {
     "altRows": true,
     "rowList": [10, 25, 50, 100],
     "sortname": "id",
     "viewrecords": true,
     "autowidth": false,
     "forcefit": false,
     "shrinkToFit": false,
     "height": "auto",
     "colModel": [{
         "index": "id",
         "editable": false,
         "name": "id",
         "label": "ID"
     }, {
         "index": "number",
         "editable": false,
         "name": "number",
         "label": "number"
     }, {
         "index": "first_name",
         "editable": false,
         "name": "client__first_name",
         "label": "first name"
     }],
     "caption": "Cars",
     "datatype": "json",
     "gridview": true,
     "sortorder": "asc",
     "viewsortcols": true,
     "url": "main/examplegrid",
     "rowNum": 10,
     "pager": "#pager",
     "jsonReader": {
         "repeatitems": false
     }
 }

 sample data = {
     "total": 1,
     "records": 1,
     "rows": [{
         "client__first_name": "Bill",
         "client__last_name": "Clinton",
         "id": 1,
         "number": "111222"
     }],
     "page": 1
 }
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
user537723
  • 93
  • 1
  • 9
  • You can fill the "virtual" column **on the client side** inside of [beforeProcessing](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:events#list_of_events) event. If you would append your question with jqGrid definition in JavaScript and with JSON data (at least one row) which will be posted to the client I would show you more detailed how you can do this. – Oleg Mar 24 '12 at 20:29
  • Here you go. config with sample data. – user537723 Mar 24 '12 at 21:11

1 Answers1

2

OK! Let us you get the JSON data

{
    "total": 1,
    "records": 1,
    "rows": [
        {
            "client__first_name": "Bill",
            "client__last_name": "Clinton",
            "id": 1,
            "number": "111222"
        }
    ],
    "page": 1
}

and the jqGrid contains an additional column

{name: "client__full_name", label: "full name"}

which should be constructed from client__first_name and client__last_name. In the case the most simplest way would be to use beforeProcessing callback function:

$("#list").jqGrid({
    url: "main/examplegrid",
    datatype: "json",
    colModel: [
        {name: "id", label: "ID"},
        {name: "client__first_name", label: "first name"},
        {name: "client__last_name", label: "last name"},
        {name: "client__full_name", label: "full name"}
    ],
    gridview: true,
    jsonReader: { repeatitems: false },
    //... other parameters
    beforeProcessing: function (data) {
        var items = data.rows, n = items.length, i, item;
        for (i = 0; i < n; i++) {
            item = items[i];
            item.client__full_name = item.client__first_name + ' ' +
                item.client__last_name;
        }
    }
});

The callback function beforeProcessing will be called by jqGrid after the data are received from the server and before the data will be processed. So in the simple way we can implement any "virtual" column.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • This works fine. Thank you Oleg. How about making the toolbar searching to work with this virtual column? – user537723 Mar 26 '12 at 17:04
  • @user537723: You are welcome! All works exactly as in case of searching in the standard column. You should just extend your server code to analyse for "client__full_name" as the input field. – Oleg Mar 26 '12 at 17:07