Reputation: 5946
I want to grab a recordset by its foreign key using the filter term. But somehow this is not working. I'm showing the models here:
class MessageMonitoring(models.Model):
MyID = models.ForeignKey(AboutMe, null=True, blank=True, related_name='MessageMonitor')
class AboutMe(models.Model):
MyGender = models.CharField(max_length=50, choices = GENDER_CHOICES)
In my view I'd like to do the following:
recipient = AboutMe.objects.get(pk=toid)
monitor_recip = MessageMonitoring.objects.filter(MessageMonitor_owner=recipient)
But I get the following error:
Cannot resolve keyword 'MessageMonitor_owner' into field. Choices are: MyID, ...
Upvotes: 0
Views: 120
Reputation: 37319
I'm not sure where you're getting MessageMonitor_owner
- that's neither a model name nor a field on the model you showed. To filter using the models and foreign key relation that you're showing, use:
monitor_recip = MessageMonitoring.objects.filter(MyID=recipient)
Or, if you don't need the recipient object for anything else in the view, save some DB use by filtering against its PK rather than retrieving the object:
monitor_recip = MessageMonitoring.objects.filter(MyID_id=toid)
Upvotes: 3