0

我想使用网格来显示库存预订数据。对于我喜欢显示类型、数量和预订日期的每个预订,可以有多种预订类型,并且类型定义存储在相关表中。

我尝试在网格中显示相关表 fk 中的字段,但网格列保持为空。在 grid.py 中,我定义了字段 type__typename 字段,我在网格中得到一列,但没有数据。id、quantity 和 reservationdate 字段在网格中正确显示。

据我从源代码中可以理解,函数lookup_foreign_key_field中的jqgrid.py,应该将网格类中的字段命名为fk__field。

我使用 Django 1.5、jquery-1.9.1 和 postgesql 9.2

非常感谢任何想法或建议。

网格.py

class StockReservationsGrid(JqGrid):
    model = StockReservations # could also be a queryset
    fields = ['id', 'type__typename','quantity', 'reservationdate'] # optional     
    caption = 'Reservations' # optional
    colmodel_overrides = {
        "id": { "editable": False, "width":10, "rowNum": 5, },  
        "type__typename": {'index': 'type__typename', 'label':'Type'},
    }

    def __init__(self, request, stockitem_id):
        super(StockReservationsGrid, self).__init__()
        self.queryset = StockReservations.objects.filter(
            type__isnull=False,warehouse__isnull=False, stockitem_id=stockitem_id)
        self.url = reverse_lazy('stock_reservations_grid', args = [stockitem_id])

视图.py

def stock_reservations_grid(request, stockitem_id):
    grid = StockReservationsGrid(request, stockitem_id)
    return HttpResponse(grid.get_json(request), mimetype="application/json") 

def stock_reservations_grid_config(request, stockitem_id):
    # build a config suitable to pass to jqgrid constructor   
    grid = StockReservationsGrid(request, stockitem_id)
    return HttpResponse(grid.get_config(), mimetype="application/json")    

网址.py

urlpatterns = patterns('Warehouse.views',       
    url(r'^stock_reservations_grid/(?P<stockitem_id>\d+)/$', 
        stock_reservations_grid, 
        name='stock_reservations_grid'),
    url(r'^stock_reservations_grid/cfg/(?P<stockitem_id>\d+)/$', 
        stock_reservations_grid_config, 
        name='stock_reservations_grid_config'),           
)

javascript

<script type="text/javascript">
$(document).ready(function() {
    $.getJSON("{% url "stock_reservations_grid_config" stockitem.id %}", function(data){
        $("#deliverydata_grid")
            .jqGrid(data)
            .navGrid('#deliverydata_pager', 
                {add: false, edit: false, del: false, view: true},
        {}, // edit options
        {}, // add options
        {}, // del options 
        { multipleSearch:true, closeOnEscape:true }, // search options 
        { jqModal:false, closeOnEscape:true} // view options 
        );
    });
});
</script>  

数据

{"rowList": [10, 25, 50, 100], 
 "sortname": "id", 
 "viewrecords": true, 
 "autowidth": true, 
 "shrinkToFit": true, 
 "height": "auto", 
 "gridview": true, 
 "sortorder": "asc", 
 "rowNum": 10, "pager": "#pager", 
 "jsonReader": {"repeatitems": false}, 
 "altRows": true, 
 "caption": "Reservations", 
 "datatype": "json", 
 "rownumbers": false, 
 "forcefit": true, 
 "url": "/warehouse/reservations/stock_reservations_grid/147/", 
 "colModel": [{"index": "id", 
               "name": "id", 
               "width": 10, 
               "rowNum": 5, 
               "editable": false, 
               "label": "ID"}, 
              {"index": "type__typename", 
               "name": "type__typename", 
               "editable": true, 
               "label": "Type"}, 
              {"index": "quantity", 
               "editable": true, 
               "name": "quantity", 
               "label": "quantity"},
              {"index": "reservationdate", 
               "editable": true, 
               "name": "reservationdate", 
               "label": "reservationdate"}]}

模型.py

class STB():
    warehouse = models.ForeignKey('Warehouse.Warehouse', null=True)
    stockitem = models.ForeignKey('Stockitems.Stockitems', null=True)
    quantity = models.DecimalField(max_digits=14, decimal_places=2, default=0) 
    transactiondate = models.DateTimeField(default=datetime.datetime.now(EST()))  

    class Meta:
        abstract = True

 class StockresTypes():
        typeno = models.IntegerField(unique=True)
        typename = models.CharField(max_length=100)

        def __unicode__(self):            
                return u'%s %s' % (self.typeno, self.typename)

class Meta:        
    ordering = ['typename']    
    app_label = 'Warehouse'        

class StockReservations(STB):
    type = models.ForeignKey('StockresTypes', null=True, db_index=True)
    reservationdate = models.DateTimeField(default=datetime.datetime.now(EST()))
    sourceid = models.IntegerField(default=0, db_index=True)


    def __unicode__(self):            
        return u'%s %s %s %s' % (
            self.type, self.warehouse, self.stockitem, self.reservationdate)

    class Meta:
        ordering = ['reservationdate', 'warehouse', 'stockitem']    
        app_label = 'Warehouse'   

json.json

 {"rows": [{"transactiondate": "2013-05-09T16:51:52.191Z", 
            "stockitem_id": 2812, 
            "id": 2169, 
            "type_id": 1, 
            "reservationdate": "2009-04-17T22:00:00Z", 
            "warehouse_id": 56, 
            "quantity": "2.00"}], 
            "total": 1, "page": 1, "records": 1}
4

1 回答 1

0

我设法通过使用捆绑在 django gems 包中的 jqgrid 解决了类似的问题:http://plfiorini.github.io/django-gems/只需下载它,解压缩并运行

python setup.py 安装

然后在 grids.py 中,删除旧的 jqgrid 导入并使用 django gems by 中的 jqgrid from django_gems.jqgrid import JqGrid。现在你的 Json 应该有外键数据了。

于 2013-05-25T20:10:25.063 回答