I see it now. Collin was absolutely right.
After the new keywords are added, the related.clear() method is called and the admin adds it's own keywords, thus erasing any changes. So it all boils down to the django.models.manipulators.AutomaticManipulator:128 I totally get it, I've solved m2ms in exactly the same way many many times before, delete them all then add the existing ones. Solves the problem of having to check which ones are to be deleted... While I see now how this works, I am still curious as to how this can be solved... a post_save hook? I found an article that details this very problem: http://www.progprog.com/articles/2007/03/03/django-when-save-is-not-safe It seems that no simple solution exists yet... //D On Nov 7, 2:25 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Wed, 2007-11-07 at 00:50 +0000, Dmitri Fedortchenko wrote: > > Example code: > > > def save(self): > > regex = re.compile("\{\{([^\}]{2,60})\}\}") > > words = regex.findall(self.body) > > self.body = regex.sub("\\1",self.body) > > super(Chapter,self).save() > > for word in words: > > if len(word.strip()) > 0: > > try: > > self.keywords.create(value=word.strip().title()) > > except Exception, e: > > print e > > print self.keywords.all() # This prints the correct keywords! > > super(Chapter,self).save() > > > The outcome of this code is that new Keywords are created, but they > > are not bound to this Chapter. > > Simply calling chapter.keywords.create(value="Test") will indeed > > create a new keyword with the value "Test" bound to the chapter. I am > > running this from the django admin btw. > > Can you explain what is meant to be going on here? > > If you are trying to save m2m relations for an object that has not been > saved yet, you're out of luck. We need to know the current object's pk > value before it can be used in a m2m table and that isn't always > available (with auto primary keys). > > Normally, if you want to adjust the m2m relations for a model, hooking > into the post_save signal is the way to go. > > > > > The problem seems to be in the > > django.db.models.fields.related._add_items method, somewhere around > > line 340. > > The fact is that the insertion query is executed, but for some reason > > it is not committed, perhaps it is a transaction issue? > > Perhaps it is. Perhaps it isn't. How have you established the query was > executed? What does the commit_unless_managed() call end up doing in > your case? It might depend on your particular setup, too. Do some more > debugging and see what shows up if you think there's a problem there. > However, do keep in mind that it's generally impossible to do everything > with m2m's inside a model's save method because of the chicken-and-egg > problem with primary keys noted above. > > Regards, > Malcolm > > -- > Tolkien is hobbit-forming.http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---
