You probably want to look at bulk_create and do all of the inserts as a
single query:

https://docs.djangoproject.com/en/dev/ref/models/querysets/#bulk-create

This will probably be seconds, if not minutes faster.

-James

On Thursday, September 4, 2014, James P <testdevp...@gmail.com> wrote:

> I have what is essentially a table which is a pool of available
> codes/sequences for unique keys when I create records elsewhere in the DB.
> Right now I run a transaction where I might grab 5000 codes out of an
> available pool of 1 billion codes using the slice operator [:code_count]
> where code_count == 5000.
>
> This works fine, but then for every insert, I have to run through each
> code and insert it into the record manually when I use the code.
>
> Is there a better way?
>
> Example code (omitting other attributes for each new_item that are similar
> to all new_items):
>
> code_count=5000
> pool_cds = CodePool.objects.filter(free_indicator=True)[:code_count]
>
>
>         for pool_cd in pool_cds:
>
>             new_item = Item.objects.create(
>                 pool_cd = pool_cd.unique_code,
>             )
>             new_item.save()
>
>
>  --
> 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+unsubscr...@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','django-users%2bunsubscr...@googlegroups.com');>
> .
> To post to this group, send email to django-users@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','django-users@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/ddb2de4d-d094-4512-9ad7-acd8bb33e627%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/ddb2de4d-d094-4512-9ad7-acd8bb33e627%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CA%2Be%2BciUJK%3DcZTPPKFUEG8-10sVaTWHmsgyxqC%3DmYt2ZcZaGKNg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to