2

Let's say I have this sqlmodel.SQLModel which has a table name Project.

My intention is to feature a record of a Project for a definite period of time, e.g. 3 days, i.e. setting it's field featured to be True, and automatically set its field featured to be False thereafter.

class Project(SQLModel):
    featured: bool = False

How can I achieve this behavior using FastAPI? Is it via background tasks or what?

Jim
  • 450
  • 2
  • 10
  • Trying to do this with FastAPI is like trying to maw your lawn with a football. That information is in a database after all. If you want to update something in a database in regular intervals, you can just use a simple cron script or something to that effect. That being said, I think this is a terrible idea to begin with and @kichik provided a _much_ more sensible suggestion below. – Daniil Fajnberg Apr 04 '23 at 17:46

1 Answers1

2

Consider using something like "featured until" instead. The field will contain a date stating the last date the project should be featured. Your feature page will then look for any projects with a "features until" date greater than today.

This way you don't need any background process so your system will be simpler and easier to maintain. It also leaves you with feature history in the database which may be useful in the future.

kichik
  • 33,220
  • 7
  • 94
  • 114