管理员搜索通用外键django

时间:2015-08-03 05:32:03

标签: python django generic-foreign-key

我试图将管理员搜索放在通用外键中的模型上。

admin.py

var configArray = new Array();
var products = [];
var services = new Array();
var chars = [];
var prd;



for(var i=0;i<2;i++){
    var product = new Object();
    product["name"] = "prod"+i+"Name";
    product["desc"] = "prod"+i+"Desc";
    product["isParent"] = "prme";

    for(var j=0;j<2;j++){
            var charr = new Object();
            charr["name"] = "prod"+i+"char"+j;
            charr["val"] = "prod"+i+"char"+j+"val";
            chars[j] = charr;
    }
    product["chars"] = chars;
    products[i] = product;

}

var ProductViewModel =  function(items) {
    this.items = ko.observableArray(items);
    this.itemToAdd = ko.observable("");
    this.addItem = function() {
        if (this.itemToAdd() != "") {
            this.items.push(this.itemToAdd()); 
            this.itemToAdd(""); 
        }
    }.bind(this);  
};

$(function(){
    $('#addProdProduct').click(function() {
        window.location.href = "#addProductPage";
    });
    $('#addProdButton').click(function() {
        addProduct();
    });
    prd = new ProductViewModel(products);
    ko.applyBindings(prd);

});

function addProduct(){
    var product = new Object();
    product["name"] = $('#prodNameId').val();
    product["desc"] = $('#prodDescId').val();
    product["isParent"] = $('#prodIsParentId').val();
    prd.itemToAdd(product);
    prd.addItem();
    window.location.href = '#';
}

我可以使用class privacySettingsAdmin(admin.ModelAdmin): fieldsets = ( (None, { 'fields': ( 'field_name', 'level', ) }), (('page/article'), { 'classes': ('grp-collapse grp-open',), 'fields': ('content_type', 'object_id', ) }), ) autocomplete_lookup_fields = { 'generic': [['content_type', 'object_id']], } search_fields = ('object_id',) 进行搜索。但我希望使用object_id对象的领域进行搜索。

离。用户有一个外键来建模。然后我想使用object_idusernamefirst_nameemail搜索该用户的隐私设置。

提前致谢。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

在models.py中:

class PrivacySettings(models.Model):
    ...
    def object_username(self):
        return self.object.username
    object_username = property(object_username)

    def object_first_name(self):
        return self.object.first_name
    object_first_name = property(object_first_name)

    def object_last_name(self):
        return self.object.last_name
    object_last_name = property(object_last_name)

    def object_email(self):
        return self.object.email
    object_email = property(object_email)

在admin.py中:

class PrivacySettingsAdmin(admin.ModelAdmin):
    model = PrivacySettings
    fieldsets = (
        (None, {
            'fields': (
                'field_name',
                'level',

            )
        }),
        (('page/article'), {
            'classes': ('grp-collapse grp-open',),
            'fields': ('content_type', 'object_id', )
        }),
    )
    autocomplete_lookup_fields = {
        'generic': [['content_type', 'object_id']],
    }
    search_fields = ('object_id', 'object_username', 'object_first_name',
                     'object_last_name', 'object_email')