Flask SQLAlchemy paginate over objects in a relationship

2024/11/17 2:34:42

So I have two models: Article and Tag, and a m2m relationship which is properly set.

I have a route of the kind 'articles/tag/' and I would like to display only those articles related to that tag

I have sorted out this problem already but I am looking for a more elegant solution including pagination but I am not able to use paginateover the articles list because it's not a queryset

I need a queryset of all articles linked to a certain tag. I have tried order_by in many different ways and failed miserably.. I wasnt able to find something useful in the docs, any ideas?

Answer

While OP's problem was lazy="dynamic", I ran into this issue with a relationship which was already using that. In my case, the problem turned out to be that I was using relationship() imported directly from sqlalchemy.orm. Instead, I needed to use db.relationship() to get the version of the method wrapped by Flask-SQLAlchemy.

https://en.xdnf.cn/q/71603.html

Related Q&A

generating correlated numbers in numpy / pandas

I’m trying to generate simulated student grades in 4 subjects, where a student record is a single row of data. The code shown here will generate normally distributed random numbers with a mean of 60 …

AttributeError: list object has no attribute split

Using Python 2.7.3.1I dont understand what the problem is with my coding! I get this error: AttributeError: list object has no attribute splitThis is my code:myList = [hello]myList.split()

Managing multiple Twisted client connections

Im trying to use Twisted in a sort of spidering program that manages multiple client connections. Id like to maintain of a pool of about 5 clients working at one time. The functionality of each clien…

using a conditional and lambda in map

If I want to take a list of numbers and do something like this:lst = [1,2,4,5] [1,2,4,5] ==> [lower,lower,higher,higher]where 3 is the condition using the map function, is there an easy way?Clearly…

Tkinter: What are the correct values for the anchor option in the message widget?

I have been learning tkinter through Message widget in Tkinter at Python Courses and Tutorials. I keep getting an error when I add the anchor option with the options presented on the site. I am being t…

Why isnt Pickle calling __new__ like the documentation says?

The documentation for Pickle specifically says:Instances of a new-style class C are created using:obj = C.__new__(C, *args)Attempting to take advantage of this, I created a singleton with no instance a…

Remove more than one key from Python dict

Is there any efficient shortcut method to delete more than one key at a time from a python dictionary?For instance;x = {a: 5, b: 2, c: 3} x.pop(a, b) print x {c: 3}

Install poppler in AWS base python image for Lambda

I am trying to deploy my docker container on AWS Lambda. However, I use pdf2image package in my code which depends on poppler. To install poppler, I need to insert the following line in the Dockerfile.…

Cant change state of checkable QListViewItem with custom widget

I have a QListWidget where I want to add a bunch of items with a custom widget:listWidget = QListWidget()item = QListWidgetItem()item.setFlags(item.flags() | Qt.ItemIsUserCheckable)item.setCheckState(Q…

Custom table for str.translate in Python 3

If I run this code:s.translate(str.maketrans({as: dfg, 1234: qw}))I will get:ValueError: string keys in translate table must be of length 1Is there a way to replace multiple characters at once using st…