One slight difference: If you need to deal with the object not being found,
you get a different error using get as opposed to using filter and then
pulling the first element out of the collection. Here's an example, with
Location being the model class:

try:
    Location.objects.filter(pk=id)[0]
except IndexError:
    print 'Not found!' # ...handle error here...

try:
    Location.objects.get(pk=id)
except Location.DoesNotExist:
    print 'Not found!' # ...handle error here...

Ben

On Wed, Jul 14, 2010 at 1:41 PM, Jonathan Hayward <
christos.jonathan.hayw...@gmail.com> wrote:

> Thank you; the first seemed to work nicely.
>
>
> On Wed, Jul 14, 2010 at 2:36 PM, ringemup <ringe...@gmail.com> wrote:
>
>> Try:
>>
>>    directory.models.Entity.objects.get(pk=id)
>>
>> Which does pretty much the same thing as:
>>
>>    directory.models.Entity.objects.filter(id__exact = id)[0]
>>
>> (note id__exact rather than id__equals)
>>
>>
>>
>> On Jul 14, 3:18 pm, Jonathan Hayward
>> <christos.jonathan.hayw...@gmail.com> wrote:
>> > How do I look up a model instance by id (its primary key)? I have:
>> >
>> >     entity = directory.models.Entity.objects.filter(id__equals = id)[0]
>> >
>> > and am getting:
>> >
>> > FieldError at /profile/1
>> >
>> > Join on field 'id' not permitted. Did you misspell 'equals' for the
>> lookup type?
>> >
>> > Is there another way I should be going about this given the id field
>> > beforehand?
>> >
>> > --
>> > → Jonathan Hayward, christos.jonathan.hayw...@gmail.com
>> > → An Orthodox Christian author: theology, literature, et cetera.
>> > → My award-winning collection is available for free reading online:
>> > ☩ I invite you to visit my main site athttp://JonathansCorner.com/
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-us...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>>
>
>
> --
> → Jonathan Hayward, christos.jonathan.hayw...@gmail.com
>
> → An Orthodox Christian author: theology, literature, et cetera.
> → My award-winning collection is available for free reading online:
> ☩ I invite you to visit my main site at http://JonathansCorner.com/
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

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

Reply via email to