My question is simple: in a Django app, I have a table Users and a table StatusUpdates. In the StatusUpdates table, I have a column user which is a foreign key pointing back to Users. How can I do a search expressing something like:
users.filter(latest_status_update.text__contains='Hello') Edit:
Please excuse my lack of clarity. The query that I would like to make is something like "Give me all the users whose latest status update contains the text 'hello'". In Django code, I would do the following (which is really inefficient and ugly):
hello_users = [] for user in User.objects.all(): latest_status_update = StatusUpdate.objects.filter(user=user).order_by('-creation_date')[0] if latest_status_update.text.contains('Hello'): hello_users.append(user) return hello_users Edit 2:
I've already found the solution but since I was asked, here are the important parts of my models:
class User(models.Model): ... class StatusUpdate(models.Model): user = models.ForeignKey(User) text = models.CharField(max_length=140) creation_date = models.DateTimeField(auto_now_add=True, editable=False) ....