Django和Extjs,如何使用jsonstore的组合

时间:2011-05-29 13:21:39

标签: django extjs jsonstore

我想用JsonStore显示一个extjs组合 服务器端我使用python / Django。

所以,这是我的组合:

xtype: 'combo',
store: new Ext.data.JsonStore({
    url: 'get_peoples',
    root: 'data',
    totalProperty: 'total',
    fields: [
        {name: 'name', mapping: 'fields.name'},
        {name: 'subname', mapping: 'fields.subname'}
    ],
    autoLoad: true
}),
trigerAction: 'all'

和views.py服务器端:

def get_peoples(request):
    queryset = People.objects.all()    
    data = '{"total": %s, "%s": %s}' % \
        (queryset.count(), 'data', serializers.serialize('json', queryset))
    return HttpResponse(data, mimetype='application/json')

get_people电话给我

{"total": 1, "data": [{"pk": 1, "model": "myapp.people", "fields": {"name": "Paul", "subname": "Di Anno"}}

我想我做得不对,因为我的组合没有显示任何项目

1 个答案:

答案 0 :(得分:2)

您需要将displayField和valueField声明添加到ComboBox,以便它知道您的商店中哪些字段用于任一角色。此外,无需在商店中设置autoLoad。

new Ext.form.ComboBox({
    xtype: 'combo',
    store: new Ext.data.JsonStore({
        url: 'get_peoples',
        root: 'data',
        totalProperty: 'total',
        fields: [
            {name: 'name', mapping: 'fields.name'},
            {name: 'subname', mapping: 'fields.subname'}
        ]
    }),
    triggerAction: 'all',

    // Just guessing at the proper fields here.
    // Set them to whatever you wish.
    displayField: 'name',
    valueField: 'subname'
});