在django admin中为search_fields添加一个choicefield

时间:2015-08-01 19:41:24

标签: django django-admin

说,我有一个带有选择字段的模型,如下所示:

STARTED = 1
DONE = 0

STATUSES = {
    (STARTED, 'Started'),
    (DONE, 'Done'),
}

status = models.IntegerField(choices=STATUSES)

我想在status中将字段search_fields = ()添加到admin.py,如下所示。

search_fields = (status,)

它按整数搜索,但我想根据显示文字进行搜索"完成"和"开始。"

1 个答案:

答案 0 :(得分:1)

您应该可以通过覆盖模型管理员的get_search_results方法来执行此操作。

using(var ctx = new MyEntities())
{
    foreach (Billing billing in ctx.Billings)
    {
        // Calculate billing price lines from billing price line specifications         
        try
        {
            foreach (BillingPriceLine priceLine in billing.BillingPriceLines)
            {
                // Declare a local variable for holding the specification total sum
                decimal specificationsSum = 0;

                // Loop through billing price line specifications on this price line
                foreach (BillingPriceLineSpecification specification in priceLine.BillingPriceLineSpecifications)
                {
                    // First, check if the estimated production and realised production has a value
                    if (specification.EstimatedProduction.HasValue &&  specification.RealisedProduction.HasValue)
                    {
                        // Calculate production for a price line specification
                        specification.Production = specification.EstimatedProduction.Value - specification.RealisedProduction.Value;

                        // Add to total specification sum
                        specificationsSum += specification.Production*specification.Price;
                    }
                }

                // Set total production on price line
                priceLine.Production = priceLine.BillingPriceLineSpecifications.Sum(x => x.Production);

                // Set price on price line
                priceLine.Price = specificationsSum/priceLine.Production;

                // Set total price on price line
                priceLine.TotalPrice = ((priceLine.Production*priceLine.Price)*priceLine.Share)/100;
            }

            // Set subtotal, VAT and total sum on billing
            billing.Subtotal = billing.BillingPriceLines.Sum(x => x.TotalPrice);
            billing.VAT = billing.Subtotal/4;
            billing.Total = billing.Subtotal + billing.VAT;
        }
        catch
        {
            // Handle error logging here ..
        }
    }

    ctx.SaveChanges();
}