tags:

views:

150

answers:

4

Basically, i've created a view to populate my database with Serial models from 0000 to 9999. below is the code i'm using for the view.

def insert_serials(request):
    for i in range(0,10000):
    serial = Serial(i,False)
    serial.save()
    else:
    print 'The for loop is over'

what is the right way to do this, and i'm getting an IntegrityError, duplicate keys, my model defination is below:

class Serial(models.Model):
    serial = models.CharField(max_length=4)
    closed = models.BooleanField()

    def __unicode__(self):
        return "%s" %(self.serial)

    def get_absolute_url(self):
        return "/draw/serial/%s/" % (self.serial)
A: 

Try adding unique=False in the closed field declaration.

Also, you're trying to put integers into a string field. You should do it like Serial('%04d' % i, False) to put values from '0000' to '9999'.

Roberto Bonvallet
i did this, but the unique index seemed to be on my Serial field, so i removed the index from the database and it did in fact insert 9999 records but all with 0 as the serial, meaning i was always zero.
Rasiel
+1  A: 

There may be positional default arguments, try using keywords:

from django.db import transaction

@transaction.commit_manually
def insert_serials(request):
    for i in range(0,10000):
        serial = Serial(serial=str(i),closed=False)
        serial.save()
    transaction.commit()
    print 'The for loop is over'

It's wrapped in a transaction should speed it up a bit. See transaction.commit_manually for details

monkut
this gave me a TransactionManagementError and only inserts one row, obviously i is not increasing, and the for loop ends after 1 record only.
Rasiel
Strange, looks simple enough. Whats the full error?
monkut
why are you doing for else? I'm pretty sure else fires only if the for never executes, which will never happen.
emeryc
I agree the 'else' statement there can be confusing, I'll remove it, but it should work fine. From the docs:Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
monkut
@monkut thanks for the heads up, I totally misunderstood what else did.
emeryc
+1  A: 

Your code is working on my site - Mac OS X, Python 2.6.3, django from trunk, sqlite3

I changed your view function code a bit, though -

from django.http import HttpResponse
from models import Serial

def insert_serials(request):
    for i in range(0,10000):
        serial = Serial(i,False)
        serial.save()
    return HttpResponse("Serials are inserted")
vorushin
A: 

Your id field (implied by the absence of a PK definition in your model) is not being autonumbered correctly and therefore every INSERT after the first is failing with a duplicate id value. What's your database? Did you have Django create the table, or did you do it yourself?

Larry Lustig
hi, i'm using mysql and yes, Django created the table using syncdb and the def's in the models.py
Rasiel
this i think was the problem, i had to manually assign PK defination on the database. There was another code problem, but this was what primarily wasn't causing the for loop to save after 1
Rasiel