Hi Tom & Tundebabzy,

I understood your idea & approach. But as I've mentioned - I receive
the model from the 3rd party service & want to save it. As I have
experience with Hibernate - it provides such features. That's why I'm
a little bit frustrated with python approach.

BR,
Dmitry

On Aug 24, 6:39 pm, Tundebabzy <tundeba...@gmail.com> wrote:
> On Aug 24, 3:36 pm, Tom Evans <tevans...@googlemail.com> wrote:
>
>
>
>
>
>
>
>
>
> > On Tue, Aug 23, 2011 at 11:28 PM, ernando <dmitry.home...@gmail.com> wrote:
> > > Hi all,
>
> > > maybe it's newbie question but I wasn't able to find clear answer/
> > > solution on it.
>
> > > For example, we have the followingmodels:
>
> > > 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 tosavethem 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 firstlysavea object - everything goes smoothly. So, the
> > > question is - should we alwayssaveall 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 tosaveit into DB withonecalland not do it for
> > > each item.
>
> > > Regards,
> > > Dmitry
>
> > Only objects that exist in the database can be related to each other.
> > If you have a function which accepts an model instance, it should make
> > it clear that it is an error to pass it an object that does not exist
> > in the database. You really should not be overridingsave() tosave
> > related objects that do not exist in the database.
>
> > Mostly these kind of problems go away if you start using the create
> > and get_or_create helpers on the model's manager, eg I would never
> > have this in my code:
>
> > a = A(title="123")
> > b = B(title="333", aItems = a)
> > b.save()
>
> > it would look like this:
>
> > a, created = A.objects.get_or_create(title='123')
> > b = B.objects.create(title='333', aitems=a)
>
> > Cheers
>
> > Tom
>
> +1

-- 
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