如何在Django的问卷系统中的单个查询中获得多个ques_id的答案?

时间:2016-02-20 11:13:26

标签: python mysql django django-orm

有3个问题 1.你的年龄是多少? 2.你的性别是什么? 你的规模? 对于哪个问题ID分别为3,6和9。

答案模型:

(function(){
//Namespacing the views collections and models
window.App = {
Models: {},
Views: {},
Collections: {},
Helpers: {}
},
//Template helper to load the template of any id    
App.Helpers.template = function(id){
return _.template($('#' + id).html());
}


//Person Model
App.Models.Person = Backbone.Model.extend({});

//Person collection - People
App.Collections.People = Backbone.PageableCollection.extend({
model: App.Models.Person,
state: {
pageSize: 10
},
mode: "client"
});


var personCollection = new App.Collections.People([
{
id: 1,
name: 'Trim',
age: 33,
occupation: 'Dotnet Programmer'
},
{
id: 2,
name: 'Crum',
age: 25,
occupation: 'Developer'
},
{
id: 3,
name: 'Drum',
age: 46,
occupation: 'Designer'
},
{
id: 4,
name: 'Srum',
age: 27,
occupation: 'Java Programmer'
},
{
id: 5,
name: 'Vrum',
age: 24,
occupation: 'Developer'
},
{
id: 6,
name: 'Brum',
age: 29,
occupation: 'Designer'
},
{
id: 7,
name: 'Frum',
age: 33,
occupation: 'Dotnet Programmer'
},
{
id: 8,
name: 'Jrum',
age: 25,
occupation: 'Developer'
},
{
id: 9,
name: 'Lrum',
age: 46,
occupation: 'Designer'
},
{
id: 10,
name: 'Hrum',
age: 27,
occupation: 'Java Programmer'
},
{
id: 11,
name: 'Prum',
age: 24,
occupation: 'Developer'
},
{
id: 12,
name: 'Zrum',
age: 29,
occupation: 'Designer'
}
]
);

var EditCell = Backgrid.Cell.extend({
template: _.template('<button>Edit</button>'),
events: {
"click": "editRow"
},
editRow: function (e) {
e.preventDefault();
//Enable the occupation cell for editing 
//Save the changes 
//Render the changes.
},
render: function () {
this.$el.html(this.template(this.model.toJSON()));
this.delegateEvents();
return this;
}
});

var columns = [{
name: "id",
label: "ID",
editable: false,
cell: Backgrid.IntegerCell.extend({
orderSeparator: ''
})
}, {
name: "name",
label: "Name",
cell: "string"
}, {
name: "age",
label: "Age",
cell: "integer"
}, {
name: "occupation",
label: "Occupation",
cell: "string" 
}, {
name: "actions",
label: "Actions",
cell: EditCell 
}];

// Initialize a new Grid instance
var grid = new Backgrid.Grid({
columns: columns,
collection: personCollection
});

var paginator = new Backgrid.Extension.Paginator({
collection: personCollection
});

// Render the grid and attach the root to your HTML document
$("#grid").append(grid.render().el);
$("#paginator").append(paginator.render().$el);

})();

Value_int包含年龄(问题ID 3)和比例(问题ID 9)的答案 和value_str保持性别的答案(问题ID 6)

我想使用个人资料ID列表查询答案模型。

我试过了:

class Answer(models.Model):

    profile = models.ForeignKey(Profile, help_text=u'The user who supplied this answer')
    question = models.ForeignKey(Question, help_text=u"The question that this is an answer to")
    value_int = models.IntegerField(null=True, blank=True)
    value_str = models.CharField(max_length=255, null=True, blank=True)

用户ID就是提交这些图书的人,但年龄,性别等级将由作者提供。因此,对于单个配置文件ID,可能存在多个年龄,性别或规模,因此获取book_name是必需的。

由于记录很多,因此需要花费大量时间才能获得结果。 我想优化它并在单个查询中获取数据, 对于每个book_name,我可以将年龄,性别和比例值一起放在不同的行中。 请帮我解决这个问题。

编辑:

我尝试使用OR条件但是结果集的问题是它给出了多行,使得具有年龄答案的行将具有性别和sacle值为none。并且性别答案行将具有年龄和比例值无。等等,它的太多数据会造成重复,有些缺失。

0 个答案:

没有答案