Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How do Django and Flask find and filter the property fields of model

Shulou Source: shulou.com Published: 2022-06-02 07:23:26 09月19日 Update

This article mainly explains "how Django and Flask find and filter the attribute fields of model". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Django and Flask find and filter the attribute fields of model.

In Django/Flask backend development, we often add or modify model. The data submitted by the front end is a dictionary. In some cases, some of the data submitted by the front end are not necessarily attribute fields of model, so we need to remove the fields that are not in model. There are usually two methods. The easiest and most convenient method is to pop the data sent from the frontend. Here is another method:

Detect the data when adding or modifying Model to filter out attribute fields that are not in the model

# for django

Def model_check (model, data): return dict ([(k, data [k]) for k in data.keys () if k in [x.name for x in model._meta.fields]])

# for flask (SQLAlchemy orm)

Def model_check (model, data): return dict ([(k, data [k]) for k in data.keys () if k in [x.name for x in model.__table__.columns]]) so far, I believe you have a deeper understanding of "how Django and Flask find and filter the attribute fields of model". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Fields properties front end data methods content learning practical deeper simple interesting dictionary practical practical that is situation simple operation more friends regular Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Apple Huawei Linux