Hello everyone!
In a project I am working on I have a model where I would like to mark few instances as “featured” which then get displayed in particular places.
I am wondering what is the solution more experienced Djangonauts would choose?
I think having a flag like is_featured = models.BooleanField
is not really optimal to filter just a couple of records out of possibly hundreds…
Another solution would be to create something like FeaturedItem
model which would have just a one-to-one mapping to the actual record and I could stored featured this way.
What is the recommended solution?
Hmmm… Part of the answer is going to depend upon how this is going to be used. Not just that you’re going to query on that field to identify a subset of items, but also determine whether there are other constraints or limitations involved.
For example, is there going to be a limit to the number of featured items? Who will have the authority to mark an item as featured or not? Is that authority different from the people who are adding or removing items? Does a featured item expire after some period of time? … or because of some external condition?
Does that “is_featured” flag itself show up in other places, to distinguish between featured and not-featured items in item lists?
You’ll want to identify the entirety of how this is going to be used and how it interacts with the rest of the site - then think about the queries or processing involved with handling each case - and consider the frequency of those operations.
Sometimes it’s a tradeoff - you may need to decide between an implementation that makes 90% of everything really easy and the other 10% very difficult, or a different implementation that makes everything somewhat average.
Bottom line is that “optimal” is rarely a yes/no answer, and it’s always extremely context sensitive - and it may even change over time as the application matures and evolves.
1 Like
Ken is right that this is very context dependent. If your queries often combine ‘featured’ with filters on other fields then you might need to think carefully about how you design things.
If you only want to select all featured items, or paginate them, then yes a boolean field can work. You can combine it with a conditional index to have the database index only the featured items, making queries for featured items fast without consumign lots of space in the index for the non-featured items.
1 Like
Thank you both!
Currently the plan is to have the “featured” items displayed on something similar to a landing page and only super-admin is allowed to modify what is featured and what not.
Big reason to ask was that I wanted to explore other options than those I already know and possibly learn something. And I have already learned about conditional indexing - I had no idea something like this is possible
So basically I could create index for items where is_featured=True
and in this way the query would be very efficient?
Actually, if you create an indexed field for an is_featured
column, it’s going to be efficient from the querying perspective either way. What you’re mostly saving with a conditional index is the space needed for the index and potentially time when marking or unmarking that column. (Also see the notes in that section - MySQL / MariaDB don’t support conditional indexes and that clause is ignored if you’re using either of those.)
But either way, in general terms it’s tough to do things “wrong” when you’re talking about a single query for a table of less than 1000 rows. For a boolean field, the index is only going to be one or two pages either way - and if it’s used frequently, it’s going to remain in the database cache.
For comparison purposes, other options include:
- A separate table for holding the list of PKs of the item table. This would provide the basis for implementing a facility for doing things like tracking history of what items have been featured, scheduled rotation of featured items, etc.
- A single row in a “data” table, with a number of columns equal to the number of featured items allowed. This would make it easier to enforce an upper limit of featured items.
I’m not saying they’re better than adding the boolean field column, just that they facilitate some potential features for managing the list of those items.
1 Like