lundi 20 juin 2016

Python Flask how not to include one specific element in a query sqlalchemy

I wrote a query:

category_books=Book.query.filter_by(category=(Book.query.filter_by(id=id).first()).category).limit(9)

it lists all books in a specific category and I want the query not to include a specific_book.

How not to include one specific element in a query?

Here is the view:

@app.route('/title')
@app.route('/title/<id>', methods=['GET', 'POST'])
def book(id=None):
    return render_template(
        'title.html',
        id=id,
        books=Book.query.all(),
        # Query for list of categories (BROWSE BY CATEGORIES)
        categories=Book.query.distinct(Book.category).group_by(Book.category),
        # Query for specific book id
        specific_book=Book.query.filter_by(id=id).first(),
        # Query for displaying books in the same category as the specific book (OTHER BOOKS IN THIS CATEGORY)
        category_books=Book.query.filter_by(category=(Book.query.filter_by(id=id).first()).category).limit(9)
                                                        # {{specific_book.category}}
)

and the html part:

<div id="others1">OTHER BOOKS IN THIS CATEGORY</div>  
        <div id="others2">  
           {% for item in category_books %}
               <tr>    
                   <a href=/title/{{ item.id }}><img src="/images/{{ item.id}}.jpg" alt="pic1"/></a>

               </tr>  
           {% endfor %}
        </div>
    </div>

Aucun commentaire:

Enregistrer un commentaire