Re: pass related_name-s into Model.objects.create()?
> > But what about 'ForeignKey's? May we pass their 'remote_name's in with > > the kwargs? > > Foreign Keys - yes. Reverse Foreign Keys - no. Point: All kwargs takes is the fields on this object. > In the case of a foreign key, just pass in the object instance that > you want your object to be related to (e.g., when creating a Question > object, you can pass in the Poll that the question belongs to). > > > If not, what's some clean way to construct everything all at once? > > Depends on what you mean by "at once". I want to take out the objects.create, and create everything in memory. So the many-to-ones - the foreign keys - go in the kwargs. o = Model(field=x, foreigner=Foreigner()) o.manys = [ Item(), Item(), Item() ] Now everything hangs in memory like it would in the database if we created all of it. Then I want o.save() to blast it all into the database. > If you're trying to assign a list of related objects, that isn't a > single operation either - what you're actually doing is setting a > foreign key value on multiple related objects. No prob - I will just create the Item() things with a foreign key to their parent, and leave the syntactic sugar. Then I will call save() on everything from top to bottom, so all the pks populate correctly. > However, all of these are just wrappers around multiple underlying > database calls. The changes won't happen "at once" unless you start > getting involved with transactions etc. I prematurely optimize so infrequently I forgot some people use it to mean "in one database call". I just meant in one big humongous statement. No prob. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Re: pass related_name-s into Model.objects.create()?
On Tue, May 18, 2010 at 12:45 PM, Phlipwrote: > Djangoists: > > The documentation for Model.objects.create(**kwargs) does not define > kwargs. It just sez "kwargs". > > I think all of our experiences would bear out "kwargs" may at least be > the model's fields. Correct. > But what about 'ForeignKey's? May we pass their 'remote_name's in with > the kwargs? Foreign Keys - yes. Reverse Foreign Keys - no. In the case of a foreign key, just pass in the object instance that you want your object to be related to (e.g., when creating a Question object, you can pass in the Poll that the question belongs to). > If not, what's some clean way to construct everything all at once? Depends on what you mean by "at once". As soon as you're dealing with foreign keys, you're dealing with more than one database operation - at the very least, you'll need to create or find the related objects. If you're trying to assign a list of related objects, that isn't a single operation either - what you're actually doing is setting a foreign key value on multiple related objects. Django doesn't provide a single wrapper call to create *and* assign reverse foreign key relations, but if you really want one, it won't be too hard to write. However, all of these are just wrappers around multiple underlying database calls. The changes won't happen "at once" unless you start getting involved with transactions etc. Yours, Russ Magee %-) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
pass related_name-s into Model.objects.create()?
Djangoists: The documentation for Model.objects.create(**kwargs) does not define kwargs. It just sez "kwargs". I think all of our experiences would bear out "kwargs" may at least be the model's fields. But what about 'ForeignKey's? May we pass their 'remote_name's in with the kwargs? If not, what's some clean way to construct everything all at once? -- Phlip http://c2.com/cgi/wiki?ZeekLand -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
pass related_name-s into Model.objects.create()?
Djangoids: The documentation for Model.objects.create(**kwargs) just sez "**kwargs". What goes in the kwargs? Anything beside the obvious - the scalar members of the model? How about one-to-many relationships? Can I do this: Author.objects.create( name='Cromskey', blogs=[ b1, b2, Blog.objects.create(title='yack yack yack') ] ) In my hypothetical kwargs, the related_name accepts an array of the target models. Do the kwargs support this convenience? Or must I simply plug the items in the old-fashioned way, after they are created? -- Phlip http://c2.com/cgi/wiki?ZeekLand -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.