flask_admin - 如何在表单下拉列表中筛选可选值?

时间:2016-01-08 14:24:58

标签: flask-admin

我有一个带有选择框的表单。如何设置过滤器以便只显示特定对象?

class XYZ(ModelView):
  column_list = ['id', 'selection']

我想放置一个过滤器,以便在创建/更新时只显示满足特定谓词的selection个对象。

1 个答案:

答案 0 :(得分:0)

尝试

from flask_admin.contrib.sqla.filters import BaseSQLAFilter

class FilterLastNameBrown(BaseSQLAFilter):
    def apply(self, query, value, alias=None):
        if value == '1':
            return query.filter(self.column == "Brown")
        else:
            return query.filter(self.column != "Brown")

    def operation(self):
        return 'is Brown'

class MyModelView(BaseModelView):
    column_filters = [
        FilterLastNameBrown(
            User.last_name, 'Last Name', options=(('1', 'Yes'), ('0', 'No'))
        )
    ]

或者来自thedocs的更多自定义SQL。 http://flask-admin.readthedocs.org/en/latest/api/mod_contrib_sqla/

try 
{
    MailMessage mailSend = new MailMessage();
    mailSend.From = new MailAddress("williams@iquo.com.ng");
    mailSend.To.Add("williams@iquo.com.ng");
    mailSend.Subject = "Contact message from www.iquo.com.ng";

    mailSend.Body = "<b>My name is: </b>" + firstName.Text + lastName.Text + "<br/>" + "<b>My phone number is: </b>" + pNumber.Text + "<br/>"
    + "<b>My email address is: </b>" + email.Text + "<br/>" + "<b>The message is: </b>" + message.Text;
    mailSend.IsBodyHtml = true;

    SmtpClient smtpClient = new SmtpClient("smtp.iquo.com.ng", 25);                
    smtpClient.Credentials = new System.Net.NetworkCredential("williams@iquo.com.ng", "password");
    smtpClient.Send(mailSend);

    firstName.Enabled = false; 
    lastName.Enabled = false;
    pNumber.Enabled = false;
    email.Enabled = false;
    message.Enabled = false;
    Response.Redirect("about-me.html");
}
catch (Exception ex)
{
    throw new Exception(ex.ToString());
}