How to put a limit into the results filtered? In my sqlite model, flask

Viewed 341

I wanted to display a table indirectly molded by the user of my web app. Unfortunately i want to limit the number of rows that is pulled from the db model so it only displays the last 10 rows inserted, so it doesnt bug out the html of the rest of the application. Right now i am pulling every data inserted, how could i limit it?

Here is the db model code

class Dado(db.Model):
id = db.Column(db.Integer, primary_key=True)
LocalidadeDB=db.Column(db.String(40),nullable= False)
MarcaDB=db.Column(db.String(40),nullable= False)
ModeloDB=db.Column(db.String(120),nullable= False)
AnoDB=db.Column(db.Integer,nullable= False)
QuilometragemDB=db.Column(db.Integer)
PrecoDB=db.Column(db.Integer,nullable= False)
CorDB=db.Column(db.String(20),nullable= False)
nome_id=db.Column(db.String(30),db.ForeignKey('UsuarioDB.NomeDaEmpresaDB'),nullable= False)

def __repr__(self):
    return f"User('{self.MarcaDB}', '{self.ModeloDB}')"

Here it is the route and the filtering function.

@app.route("/TerceiraJanela")
  def TerceiraJanela():
TabelaTitulo = ("Marca", "Modelo", "Ano", "Quilometragem" , "Preço" , "Cor" , "Local"  )

return render_template("TerceiraJanela.html", title = "TerceiraJanela", TabelaTitulo =TabelaTitulo,Query=Dado.query.filter_by(nome_id = current_user.NomeDaEmpresaDB).all())

And here is a pic of the table that can become ginarmous.

Also bcs it is pretty

2 Answers

Just slice the query result (using [-10:] at the end):

return render_template("TerceiraJanela.html", title = "TerceiraJanela", TabelaTitulo =TabelaTitulo,Query=Dado.query.filter_by(nome_id = current_user.NomeDaEmpresaDB).all()[-10:])
Related