I can't figure out the django qeryset for retriving a value from a field by a condition

24 views
Skip to first unread message

Ali Hayder

unread,
Dec 23, 2013, 7:27:26 PM12/23/13
to django...@googlegroups.com
HI, 
My model like this 
 
        mobile_name = models.CharField("Mobile Name", max_length=50)
opinion = models.CharField('Comments', max_length=2000)
total = models.FloatField('Total Mark')


the sql qery should like this "SELECT total FROM tablexyz WHERE mobile_name=xyz"

Thanks in advance

Russell Keith-Magee

unread,
Dec 23, 2013, 8:13:09 PM12/23/13
to Django Users

Hi Ali,

The syntax you're looking for is:

MyModel.objects.filter(mobile_name='xyz')

The Django tutorial provides a good explanation of the sorts of features that are available in Django ORM queries.

Yours,
Russ Magee %-)



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/faf3674f-c1c6-41f6-98c6-a01b12d5d6f9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Message has been deleted

Ali Hayder

unread,
Dec 24, 2013, 11:12:27 AM12/24/13
to django...@googlegroups.com
Hi Russ Magee
Thanks for your answer. I am confused a little bit. 
I think  "MyModel.objects.filter(mobile_name='xyz')" line of code is like "SELECT * from table-xyz WHERE mobile_name=xyz"
but I was looking for the alternative of "SELECT total FROM tablexyz WHERE mobile_name=xyz"

Thanks in advance

C. Kirby

unread,
Dec 24, 2013, 11:59:16 AM12/24/13
to django...@googlegroups.com
Ali,
As a general statement, the django ORM will return an entire instance of a model that you can then read values off of. To print  the "total" field for each instance of the model with name "xyz" you would do:

for mm in MyModel.objects.filter(mobile_name='xyz'):
    print mm.total

While thinking about the underlying sql can be useful,it can cloud how django ends up representing the data as models.

Ali Hayder

unread,
Dec 24, 2013, 12:35:14 PM12/24/13
to django...@googlegroups.com
Hi, Kirby 
I found something. Is it will work? 
MyModel.objects.filter(mobile_name='xyz').values(total)

Thanks for your support
Reply all
Reply to author
Forward
0 new messages