Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 4, 2005 22:52:31

w.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using API to bulk populate data


Love the application but the tutorial and API reference only show
fairly simple class create and adds.

What's the syntax to change or populate ForeignKey and ManyToMany
fields?

What's the best way to bulk load 100s or 1000s of classes which have a
variety of ForeignKeyFields and ManyToManyFields?

thanks --- warren.

Offline

#2 Nov. 4, 2005 23:08:46

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using API to bulk populate data


On 11/4/05, wrb <> wrote:
> What's the syntax to change or populate ForeignKey and ManyToMany
> fields?

See the documentation:http://www.djangoproject.com/documentation/db_api/#relationships-joinshttp://www.djangoproject.com/documentation/models/many_to_one/http://www.djangoproject.com/documentation/models/many_to_many/> What's the best way to bulk load 100s or 1000s of classes which have a
> variety of ForeignKeyFields and ManyToManyFields?

The most efficient way is to do it in your database directly, using
something like PostgreSQL's "COPY" statement. If you're not worried
about efficiency, just use the API to insert one record at a time.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#3 Nov. 7, 2005 15:52:07

w.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using API to bulk populate data


Adrian Holovaty wrote:
> On 11/4/05, wrb <> wrote:
> > What's the syntax to change or populate ForeignKey and ManyToMany
> > fields?
>
> See the documentation:
>
>http://www.djangoproject.com/documentation/db_api/#relationships-joins>http://www.djangoproject.com/documentation/models/many_to_one/>http://www.djangoproject.com/documentation/models/many_to_many/>
> > What's the best way to bulk load 100s or 1000s of classes which have a
> > variety of ForeignKeyFields and ManyToManyFields?
>
> The most efficient way is to do it in your database directly, using
> something like PostgreSQL's "COPY" statement. If you're not worried
> about efficiency, just use the API to insert one record at a time.
>
> Adrian
>
> --
> Adrian Holovaty
> holovaty.com | djangoproject.com | chicagocrime.org

Ok....

I figured out how to create the primary object one at atime with
ForiegnKeys
by setting the appropriate combination of relatedclass_id and
relatedclass.get_id(key__exact..) calls. The problem now is with its
cooresponding ManyToManyFields (These are not needed at create time)

The reference guide and tutorial refer to using
class.add_relatedclass(...) calls but
that fails when I try to use these. python says:

AttributeError: object has no attribute add_relatedclass

I do, however, see methods for **set_relatedclass** when I dir the
primary class. The
documentation, to the best of my knowledge, doesn't mention how to use
these
methods.

Offline

#4 Nov. 7, 2005 16:13:12

Jacob K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using API to bulk populate data


I do, however, see methods for **set_relatedclass** when I dir the
primary class. The
documentation, to the best of my knowledge, doesn't mention how to use
these
methods.Check outhttp://www.djangoproject.com/documentation/models/many_to_many/. The basic syntax is ``obj.set_realtedclasses(list_of_related_class_ids)``.Jacob

Offline

#5 Nov. 8, 2005 00:07:11

K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using API to bulk populate data


I'm working on a similar script to convert a legacy database. Just a
hint from a tough lesson learned, you work the opposite order that OO
may teach you and you save() first, then aggregate.

So example:
class PhoneNumber(meta.Model):
number = meta.CharField()

class Contact(meta.Model):
name = meta.CharField()
home_phone = meta.ForeignKey(PhoneNumber)
work_phone = meta.ForeignKey(PhoneNumber)


So to build a Contact, do the following:
home = PhoneNumber(number = '555-12-12')
home.save()

work = PhoneNumber(number = '555-1212')
work.save()

c = Contact(name = 'Bob', home_phone = home, work_phone = work)
c.save()

Offline

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support