简体   繁体   English

如何以及在何处删除 Flask - SQLAlchemy 中的行

[英]How and where do I delete rows in Flask - SQLAlchemy

I'm building a project using flask and flask-SQLAlchemy (with sqlite3 and with python 3.8).我正在使用 flask 和 flask-SQLAlchemy(使用 sqlite3 和 python 3.8)构建一个项目。 I have models.py which holds all the tables of the db, and I want to have static method which deletes rows in Articles table depends on one of their attribute.我有models.py,其中包含数据库的所有表,并且我想要删除Articles 表中的行的static 方法取决于它们的属性之一。

I thought about writing this funciton in the models class and wanted to ask if that's fine.我想在模型 class 中编写这个函数,并想问这是否可以。 The function looks like: function 看起来像:

def update_articles():
    all_articles = Articles.query.all()
    for article in all_articles:
        if needs_to_be_deleted(article):
            db.session.delete(article)
            db.session.commit()

Is that funciton fine (don't mind the needs_to_be_deleted(article) thing).那个功能好吗(不要介意needs_to_be_deleted(article) 的事情)。 Is the way I delete the article good?我删除文章的方式好吗? and is the place for this function can be at the models.py file?这个 function 的位置可以在 models.py 文件中吗?

As per my experience its all good here.根据我的经验,这里一切都很好。 As a general rule you should design your application as:作为一般规则,您应该将您的应用程序设计为:

  1. Models should do the most powerful stuff模型应该做最强大的事情
  2. Views should carry out only logical work视图应该只执行逻辑工作
  3. Templates should be dumb.模板应该是愚蠢的。 They should just render things.他们应该只渲染东西。

Now by saying the above i mean to say all the stuff relating to the operations with database should be in the models.现在说上面我的意思是说所有与数据库操作相关的东西都应该在模型中。 Just like you are doing.就像你正在做的那样。 Most of the time all the transactions with your database are simple ones and can be written with a couple of lines with ORM.大多数情况下,数据库的所有事务都是简单的事务,可以用 ORM 用几行代码编写。 But sometimes if you feel that something needs to be done over and over again or you feel like having a method for doing something big.但有时如果你觉得某事需要一遍又一遍地完成,或者你觉得有一种方法可以做大事。 Then its better to have that in your models only.那么最好只在你的模型中使用它。 The method you mentioned should be a part of your Article model.您提到的方法应该是您文章 model 的一部分。 Other things that can be there in your model could be operations like sending emails to users or some other routine tasks. model 中的其他内容可能是向用户发送电子邮件或其他一些日常任务等操作。

As far as your views are concerned.就你的观点而言。 They should carry out all the logical stuff.他们应该执行所有合乎逻辑的事情。 Your business logic is basically implemented by the views.您的业务逻辑基本上是由视图实现的。

Lastly templates should do the work of rendering things whatever is fed to them by the views.最后,模板应该负责渲染视图提供给它们的任何东西。

Obviously there are no such hard and fast rule.显然没有这样的硬性规定。 There can be exceptions to the above.上述情况可能有例外。 Or someone could find any other way of doing things better rather than that i mentioned.或者有人可以找到比我提到的更好的其他方式来做事。 For that you need to use your own conscience and no one can teach you that.为此,您需要使用自己的良心,没有人可以教您。 It comes with experience.它带有经验。

For your reference please go through the following articles:请通过以下文章 go 供您参考:

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

https://flask.palletsprojects.com/en/1.1.x/tutorial/ https://flask.palletsprojects.com/en/1.1.x/tutorial/

Also you might be using this but in case your aren't: https://flask-marshmallow.readthedocs.io/en/latest/您也可能正在使用它,但如果您不是: https://flask-marshmallow.readthedocs.io/en/latest/

Just one small suggestion too.也只是一个小建议。 though i am not clear about your requirements.虽然我不清楚你的要求。 But it would be better if you could commit after the for loop ie at the very end of the method.但是,如果您可以在for 循环之后提交,即在方法的最后提交,那会更好。 A request should generally have only one commit.一个请求通常应该只有一个提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM