Probably because I was setting the ForeignKey in the new_cmd_str to
the new_eda_app that was in the outer loop.  That created a new
reference to that instance, and Django knew that it would need a new
primary key.  Without another external reference, it seemed that the
same primary key was being used.

Would this explanation seem reasonable?

On Dec 8, 12:52 pm, Marc Edwards <jmarcedwa...@gmail.com> wrote:
> OK...adding the .pk=None did the trick, but why didn't I have to do
> this on the outer loop?
>
> On Dec 8, 11:52 am, Brian Schott <bfsch...@gmail.com> wrote:
>
>
>
>
>
>
>
> > 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
> > bfsch...@gmail.com
>
> > 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_cat
> > >  egory=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 django-users@googlegroups.com.
> > > To unsubscribe from this group, send email to 
> > > django-users+unsubscr...@googlegroups.com.
> > > For more options, visit this group 
> > > athttp://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 django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to