I’ve got an object that have the counts for connections in twitter and facebook saved in the object’s table. I’m trying to sort a list of these objects by the combined sum of the facebook and twitter counts using the .extra field.
Some of the twitter / facebook fields are empty (None) so the combined is blank. This is causing some issues, and these blank ones are being sorted first. Is there any way to have these blank fields sorted as if they were 0?
pages = Page.objects.filter(
setup_complete=1, active=1
).exclude(
Q(background_image__isnull=True) | Q(background_image__exact='')
).extra(
select={'total_count':'twitter_count + facebook_count'},order_by=['-total_count']
)[:99]
This is not tested, but can you do this?
If not, you may need to do it in the model. For example, in the model add a
Then in your query: