Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're not sure whether the things in your <code>item_list</code> already exist in your DB, and you need the model objects, then <code>get_or_create</code> is definitely the way to go. </p> <p>If you know the items are NOT in your DB, you would be much better doing:</p> <pre><code>for item in item_list: new = Entry.objects.create( field1 = item.field1, field2 = item.field2, ) </code></pre> <p>And if you don't need the objects, then just ignore the return from the function call. It won't speed the DB stuff, but it will help with memory management if that's an issue.</p> <p>If you're not sure whether the data is already in the DB, but either field has a <code>unique=True</code> flag on it, then the DB will enforce the uniqueness, and you can just catch the exception and move on. This will prevent an extra DB hit by avoiding the attempt to select the existing object.</p> <pre><code>from django.db import IntegrityError for item in item_list: try: new = Entry.objects.create( field1 = item.field1, field2 = item.field2, ) except IntegrityError: continue </code></pre> <p>You could increase speed in either case by manually managing the transactions. Django will automatically create and commit a transaction for every save, but provides some decorators that will greatly increase efficiency if you know you'll be doing a lot of DB saves in a particular function. The Django docs do a better job of explaining all of this than I can here, but you'll probably want to pay particular attention to <a href="http://docs.djangoproject.com/en/1.1/topics/db/transactions/#django-db-transaction-commit-on-success" rel="nofollow noreferrer">django.db.transaction.commit_on_success</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload