It seems that there is no way to make this work yet with the current
admin.

The idea is that when you are writing an article, you might mark
certain words in that article to become keywords. When saving the
article, these keywords are created (if they do not exist) and then
added as m2ms to the article.
I placed this code into the model's save method, however there is
currently no way to do it with the django admin since all m2ms are
cleared after the save() method...

So I cheated, the admin list_display variable can call methods, so I
dumped the code into
a method called by the admin list_display. So whenever a list is
displayed this method is called and thus the body of the article is
scanned for keywords to be added. Not the most optimal solution, but
the only one I could think of.

Still, the most logical course is to override the save method.

//D

On Nov 7, 1:50 am, Dmitri Fedortchenko <[EMAIL PROTECTED]> 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.
>
> 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?
>
> It is simply broken when called from within the save() method of a
> Model instance...
>
> I haven't started a ticket because I am not sure if this is bug or a
> feature ;-)


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

Reply via email to