Tuesday, August 23, 2011

saving several models with one save() call

Hi all,

maybe it's newbie question but I wasn't able to find clear answer/
solution on it.

For example, we have the following models:

class A(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=30)

class B(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=30)
aItems = models.OneToOneField(A)

And try to save them in the following way:

a = A(title="123")
b = B(title="333", aItems = a)
b.save()

This code runs with the error: (1364, "Field 'aItems_id' doesn't have
a default value")
if I firstly save a object - everything goes smoothly. So, the
question is - should we always save all related objects manually?
According to django docs we have to create object at first. But that's
now always convenient - e.g. I receive full model from the 3rd part
service and want to save it into DB with one call and not do it for
each item.

Regards,
Dmitry

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate