如何使烧瓶中的数据库过滤更有效?

How to make filtering on db in flask more efficient?

这里是 Flask 初学者

我有一个将数据存储在数据库中的烧瓶应用程序。我正在尝试根据 country_codeprice_type 等用户参数过滤数据库

现在,我在database.py中有多个过滤函数如下:

class DatabaseService:

    @staticmethod
    def read_list():
        items = Price.query.all()
        return {
            "items": items
        }

    @staticmethod
    def filter_prices_cc(country_code):
        if country_code is not None:
            items = Price.query.filter(country_code == country_code)
        return {
            "items": items
        }

    @staticmethod
    def filter_prices_pt(price_type):
        if price_type is not None:
            items = Price.query.filter(price_type == price_type)
        return {
            "items": items
        }

我在 controller.py 中调用这些方法如下:

@ns.route("/")
class Prices(Resource):
    def get(self):
        country = request.args.get('country_code')
        price_type = request.args.get('price_type')
        if country is not None:
            return DatabaseService.filter_prices_cc(country)
        if price_type is not None:
            return DatabaseService.filter_prices_pt(price_type)
        else:
            return DatabaseService.read_list()

是否有更有效的方法来更改过滤方法,以便根据 request.args.get() 过滤数据库?类似于:在 database.py 中定义一个过滤方法,这从 request.args.get() 中获取值并过滤数据

我必须得到以下场景:

  1. 如果用户输入country_code,则根据它过滤数据库
  2. 如果用户输入price_type,则根据它过滤数据库
  3. 如果 price_typecountry_code 都由用户输入,则按它们组合过滤数据库
  4. 如果输入了 none 个值,则必须显示整个值列表

示例数据:

{
    "items": [
        {
            "id": 1,
            "value": 21.4,
            "start": "2020-05-12T00:00:00+02:00",
            "end": "2020-05-12T01:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 2,
            "value": 18.93,
            "start": "2020-05-12T01:00:00+02:00",
            "end": "2020-05-12T02:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 3,
            "value": 18.06,
            "start": "2020-05-12T02:00:00+02:00",
            "end": "2020-05-12T03:00:00+02:00",
            "country_code": "LU",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 4,
            "value": 17.05,
            "start": "2020-05-12T03:00:00+02:00",
            "end": "2020-05-12T04:00:00+02:00",
            "country_code": "DE",
            "price_type": "TODAY"
        }]}

我不确定这是否正确,但您可以将 request.args.get() 的值作为 **kwargs:

传递

database.py 看起来像:

class DatabaseService:
    @staticmethod
    def read_list(**filters):
        items = Price.query.filter_by(**filters).all()
        return {
            "items": items
        }

controller.py为:

@ns.route("/")
class Prices(Resource):
    def get(self):
        country = request.args.get('country_code')
        price_type = request.args.get('price_type')
        return DatabaseService.read_list(**request.args)

根据您是否有 country_codeprice_type,过滤应该有效

您可能会感兴趣:

flask sqlalchemy query with keyword as variable