A little something like this.... I use a much more complex version of
this for migration.


open csv

records = dict()

for line in csv:
  parse if new table if yes continue
  parse table field names if yes continue

  if tablename == 'asdf':
     asdf = line but [delete id, set reference records to Null)
     newid = db[tablename].insert(**asdf)

     records[tablename][line.id] = newid

close csv

for each table with references:
   for each record in table
      record.reference = records[tablename][line.id]




--
Thadeus





On Sun, Sep 26, 2010 at 1:28 AM, mdipierro <mdipie...@cs.depaul.edu> wrote:
> This is a good idea. This could also be used to fix 'list:reference'
>
> On Sep 26, 1:26 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
>> Write a custom csv importer. use the code in gluon as an example for parsing.
>>
>> During the initial import, set the reference to None. While importing,
>> keep a dictionary in memory that can hold all needed references to a
>> record. After import, commit, and then go through your dictionary
>> fixing all of the references to what they should be, according to the
>> dictionary.
>>
>> This might require you to use something other than the record ID
>> though, like something you can query to get the real id. This way if
>> your csv is not fully 100% auto incremented you should be fine.
>>
>> --
>> Thadeus
>>
>> On Sat, Sep 25, 2010 at 6:41 PM, mdipierro <mdipie...@cs.depaul.edu> wrote:
>> > There is no way to do this. When you import the first record, the
>> > second does not exist yet therefore the reference cannot be adjusted
>> > by web2py. This is why self references are never a good idea. Use a
>> > link table instead.
>>
>> > On Sep 25, 2:19 pm, Jurgis Pralgauskis <jurgis.pralgaus...@gmail.com>
>> > wrote:
>> >> Hello,
>>
>> >> I have Topics Tree like this
>>
>> >> db.define_table("Topics",
>> >>           Field("parent_id","reference Topics", label=T("Parent
>> >> Topic"),
>> >>                 #~ widget=my_hierarchical_options_widget, # probably
>> >> overriden by requires
>> >>                 requires=IS_EMPTY_OR(IS_IN_DB(db,
>> >> 'Topics.id','Topics.name'))
>> >>           ),
>> >>           ....
>> >> )
>>
>> >> after exporting I get
>>
>> >> Topics.id,Topics.parent_id, ...
>> >> 10,10,...
>> >> 11,10,...
>>
>> >> but after reimporting the first record looses parent_id
>> >> I can see by exporting again:
>> >> 27,10,...
>> >> 28,27,...
>>
>> >> I understand that it should have some hook to update its reference
>> >> after it learns it's id.
>> >> probably this is not very often way to model sth but is probably a bug
>> >> -
>>
>> >> ps.: I was defining "root" topics this way, but I found workaround to
>> >> define root's by parent_id=None ;)
>>
>>
>

Reply via email to