Sunday, February 26, 2012

Re: Using query set in views.py

On Sunday, 26 February 2012 10:33:32 UTC, St@n wrote:
Sorry if i have misled anyone.

Here's a better view on what i'm asking.

def get_keyword():
   return Keyword.objects.all()

That method above returns me a list of Keyword items correct?

How then do i get a specific item from that list?

Keyword is a table in my database but i need a specific column's information in my views.py.

Hope that makes it clearer.

Best Regards,

Stanwin Siow


Presuming you mean a specific row rather than a specific column, you use one of the queryset filtering or slicing options.

eg to get the Keyword whose name field is 'foo':
    Keyword.objects.get(name='foo')

or to get all the Keywords whose name field starts with 'bar'
    Keyword.objects.filter(name__startswith='bar')

or to get the first Keyword in the database:
    Keyword.objects.all()[0]

etc.
As always, all this is explained in the tutorial and fully documented in the query method reference.
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/37AUBVVMBCYJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment