I think if you set: new_cmd_str.id = None That will force a new PK. Not sure why the CmdString constructor isn't clearing this..
Brian Schott [email protected] On Dec 8, 2011, at 11:47 AM, Marc Edwards wrote: > I'm looping through some JSON code and generating new model instances > that I am saving to my SQLiteDB. > > I have 3 nested "for" loops, like this: > > for app_catalog in JSON_Object['TSS_WorkFlow_Catalog']: > for eda_app in app_catalog['EDA_App']: > new_eda_app = > EDA_App(isv=eda_app['isv'],product_name=eda_app['product_name'],product_category=eda_app['product_category']) > new_eda_app.save() > for cmd_str_pair in eda_app['CmdStrings']['name']: > new_cmd_str = CmdString(name=cmd_str_pair['Text'], > cmd=cmd_str_pair['Value'], eda_app=new_eda_app) > new_cmd_str.save() > > My JSON file has 3 EDA_Apps, each EDA_App has 3, 4, and 4 CmdStrings, > respectively. > > When I loop through the second nested "for" for the CmdStrings, > the .save() is writing to the same model instance, and at the end of > the first nested "for" loop, I have 3 CmdString instances in my DB, > NOT 3+4+4=11 CmdString instances. > > Since my DB has 3 EDA_Apps in the DB, I think that the code to > instance a new model instance to save to the DB seems to be fine. It > is the second nested "for" loop that is acting fishy. I don't know if > I am doing something wrong relative to the DB, because when I step > through the 2nd nested "for" loop, I can clearly see the "new_cmd_str" > taking on the next values from the JSON file. This must be related to > how I am using the DB. > > When I try using .save(force_insert=True) on the 2nd nested "for" loop > save, I receive a Django DEBUG page message that the "PRIMARY KEY must > be unique". > > Any thoughts or observations on my code style? > > Kind regards, Marc > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

