How do Django and Flask find and filter the property fields of model
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!