user1592380
user1592380

Reputation: 36217

Trying to filter by field value with django database API

I'm using the django console to try to test a query on my sqllite DB.

The table's model starts with:

class Message(models.Model):
    mailbox = models.ForeignKey(Mailbox, related_name='messages')
    subject = models.CharField(max_length=255)
    message_id = models.CharField(max_length=255)
    in_reply_to = models.ForeignKey(
        'django_mailbox.Message',
        related_name='replies',
        blank=True,
        null=True,
    )
    ........

Here is my attempt at testing in the django console:

>>> from django_mailbox.models import Message
>>> o = Message.objects.all()
>>> o
[<Message: Get Gmail for your mobile device>, <Message: Tips for using Gmail>, <Message: Welcome to Gmail>, <Message: Getting started on Google+>, '...(remaining elements truncated)...']
>>> o = Message.objects.filter(in_reply_to > 0)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'in_reply_to' is not defined

As you can see Message.objects.all() works. But when I try to filter by a particular field having a positive value I get the error. What am I doing wrong?

Upvotes: 0

Views: 114

Answers (1)

David Ray
David Ray

Reputation: 76

The syntax of your field lookup is incorrect. I suggest you review the documentation for the gt lookup.

In your particular case:

o = Message.objects.filter(in_reply_to_id__gt=0)

Upvotes: 2

Related Questions