I'm fairly new to Django and was following a tutorial. A little ways through, I decided to make my own ordering for a model, but this involves a custom function.
This section of the program involves taking all the rooms and ordering them vertically on the page. Since the model is quite simple, I hope you can deduce the basic gist of the Room model as I am not very good at explaining (the entire model isn't shown but I think the relevant parts are). I want to order the rooms by the number of participants in the room. The function get_participants() defined at the bottom of the model does the work of getting the number of participants, however I cannot figure out how to apply this.
class Room(models.Model):
host = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=200)
participants = models.ManyToManyField(
User, related_name='participants', blank=True)
def get_participants(self):
return len(self.participants.all())
Here is one of the view functions where I have attempted to implement this function for sorting. I read somewhere that you can use the built-in python sorted() method on QuerySets, though I'm not sure this is the case.
def home(request):
q = request.GET.get('q') if request.GET.get('q') != None else ''
rooms = sorted(Room.objects.filter(
Q(topic__name__icontains=q) |
Q(name__icontains=q) |
Q(description__icontains=q) |
Q(host__username__icontains=q)
), key=lambda room: room.get_participants())
It would be nice if I could implement the ordering into the model Meta, but if not any way of ordering using a custom function or achieving the desired result would be nice.