Wednesday, March 30, 2011

Re: check for existence in bulk

On Mar 30, 2011, at 9:47 AM, Calvin Spealman wrote:

> I think your best bet is
>
> MyModel.objects.filter(some_field__in=ok_values).count() == len(ok_values)
>
> On Wed, Mar 30, 2011 at 12:44 PM, dmitry b <dmitry.maven@gmail.com> wrote:
>> Hi,
>>
>> is there a way to check in bulk for record existence and get back a
>> map of results similar to what's returned by in_bulk(). In my case, I
>> need to look up records by a unique field which is not the primary key
>> and I don't want object instances back, just true or false.

You could use values_list and flat as in:

User.objects.filter(username__in =['jason', 'was', 'here']).values_list('username', flat=True)

returns a list:

[u'jason', u'was']

After that you can figure out the best way to perform the lookup.

Jason


>>
>>
>> Thanks
>> Dmitry

--
You received this message because you are subscribed to the Google Groups "Django users" group.
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