Chapter 12. Followers

Socially aware web applications allow users to connect with other users. Applications call these relationships followers, friends, contacts, connections, or buddies, but the feature is the same regardless of the name, and in all cases involves keeping track of directional links between pairs of users and using these links in database queries.

In this chapter, you will learn how to implement a follower feature for Flasky. Users will be able to “follow” other users and choose to filter the blog post list on the home page to include only those from the users they follow.

Database Relationships Revisited

As we discussed in Chapter 5, databases establish links between records using relationships. The one-to-many relationship is the most common type of relationship, where a record is linked with a list of related records. To implement this type of relationship, the elements in the “many” side have a foreign key that points to the linked element on the “one” side. The example application in its current state includes two one-to-many relationships: one that links user roles to lists of users and another that links users to the blog posts they authored.

Most other relationship types can be derived from the one-to-many type. The many-to-one relationship is a one-to-many looked at from the point of view of the “many” side. The one-to-one relationship type is a simplification of the one-to-many, where the “many” side is constrained to only have at most one element. The only ...

Get Flask Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.