I’m learning django freshly i.e am a beginner, and recently i ran into a problem while adding search functionality, i want the function to consider user names into the filter everytime someone runs a search like it does with description, room name and topic name, unfortunately the user is not a textfield or charfield. please guide.
Here’s the function:
def home(request):
q = request.GET.get('q') if request.GET.get('q') != None else ''
#.objects looks for all objects of class room in database.
#.filter get all the objects that contain what the filter is looking for
rooms = Room.objects.filter(
Q(topic__name__icontains=q) |
Q(name__icontains = q) |
Q(description__icontains = q)
)
#.all() gets all objects
topic = Topic.objects.all()
context = {'rooms': rooms, 'topics': topic}
return render(request, 'meBase/home.html', context)
Here’s the room model:
class Room(models.Model):
host = models.ForeignKey(User, on_delete= models.SET_NULL, null=True)
topic = models.ForeignKey(Topic, on_delete= models.SET_NULL, null=True)
name = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
#participants =
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add= True)
class Meta:
ordering = ['-updated', '-created']
def __str__(self):
return self.name
Any help would be Greatly appreciated, thank you for your time.