On Tue, 2007-12-25 at 14:34 -0800, grassoalvaro wrote:
> I was searching for solutions but i didnt find anything.
> So, here is my problem.
> 
> I have models, for example:
> 
> class Photo(models.Model, helpers.AdminOptions):
>     name = models.CharField(default="", null=True, blank=True,
> max_length=255)
> 
> class Gallery(models.Model, helpers.AdminOptions):
>     name = models.CharField(max_length=255, null=True, blank=True)
>     photos = models.ManyToManyField(Photo, blank=True)
> 
> and i'm traing to override save() method for Gallery:
> 
>     def save(self):
>         # some code
>         super(Gallery, self).save() # we need id of new gallery
>         # some code
>         photo = Photo()
>         photo.save()
>         self.photos.add(photo) # why this doesn't work?
>         self.save() # this is not working
>         return self
> 
> photo = Photo() and photo.save() work fine, but i want to add new
> photo to Gallery (self.photos) and here is the problem. super(Gallery,
> self).save() and self.save() do not saving relations betwen Photo and
> Gallery. I waste big part of my time investigating why, but i still
> dont know that...

There are two problems here. Firstly the self.save() line can't possibly
work because it will lead to infinite recursion... it's calling itself
unconditionally every time.

Slightly trickier is the self.photos.add() line. The problem here is
that self.photos.add() calls self.save() as part of its imlpementation,
which again will lead to recursion problems. You can't add to a
many-to-many inside a save() method very easily like this. It is going
to take some redesigning of the save() path for that to be fixed
(possibly adding another flag parameter to save() or something). It will
probably be fixed on day, but at the moment, you should probably use a
signal (using the "post-save" signal) to do this external processing. 

Regards,
Malcolm

-- 
On the other hand, you have different fingers. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to