Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report