python - django - get_or_create() is not saving to sqlite db -


i trying save multiple objects in loop.

for in range(0,12):         some_url = "blabla.com/signnum=" + str(i)         req = urllib2.request(astro_url,headers=hdr)         str_response = urllib2.urlopen(req)         json_str = json.load(str_response)         transaction.atomic():             if == 0:                 capricorn, cp_created = capricorn.objects.get_or_create(astro_date=json_str['daily']['data_date'],                         defaults={                                   'headline': json_str['daily']['headline'],                                   'headcontent':json_str['daily']['content'],                                   'rating':json_str['daily']['rating'],                                   'love_rate':json_str['daily']['love'],                         })                 if cp_created:                     print 'capricorn saved'                 else:                     print 'capricorn in db'             if == 1:                 #different model             if == 2:                 #different model             ....             .... 

and goes on till 12. same fields different model names.

the problem having last 12th object not being inserted db, although

if last_created:      print last_createdobj.id      print 'lastmodelobject saved' else:      print 'lastmodelobject in db' 

is printing 1 , lastmodelobject saved. other objects being inserted.

what happenning here? loop looping 12 times. , writing db happenning atomic. can go bad here? stuck..

i using sqlite (dev) , printing id 1 doesnot exist in db, heck?

instead of 12 if statements, , 12 different models, recommend change in models structure. something this:

#models.py  capricorn = 'capricorn' aquarius = 'aquarius' pisces = 'pisces' aries = 'aries' taurus = 'taurus' gemini = 'gemini' cancer = 'cancer' leo = 'leo' virgo = 'virgo' libra = 'libra' scorpio = 'scorpio' sagittarius  = 'sagittarius'  sign_choices = (     (1, capricorn,      2, aquarius,      3, pisces,      4, aries,      5, taurus,      6, gemini,      7, cancer,      8, leo,      9, virgo,      10, libra,      11, scorpio,      12, sagittarius)   )  class sign(models.model):     signtype = models.charfield(choice=sign_choices)     astro_date = models.datefield()      headline = models.charfield(max_length=255)     headcontent = models.textfield()     rating = ratingfield()         love_rate = models.charfield(max_length=255) 

and in views, can assign index+1 signtype , get_or_create work fine you.

example:

astro_date = json_str['daily']['data_date'] sunsign, created = sign.objects.get_or_create(signtype=index+1, astro_date=astro_date,                         defaults={                                   'headline': json_str['daily']['headline'],                                   'headcontent':json_str['daily']['content'],                                   'rating':json_str['daily']['rating'],                                   'love_rate':json_str['daily']['love'],                         }) 

this way, code cleaner, , easier debug.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -