5

Going nuts over here... From within the shell, I can do:

product.tags.add("a_new_tag")

The tag is added to the db, and the tag association with the product works correctly. (i.e., when I do Product.objects.filter(tags__name__in=["a_new_tag"] the appropriate product spits out)

What I need to do is add some tags in the admin when the form is processed.

Here is my form code (read the comments in lines 4 and 5):

class ProductForm(ModelForm):
        def save(self, commit=True):
            product = super(ProductForm, self).save(commit=False)
            product.type="New Type to Confirm Info is being Saved Correctly" //this is saved to the product.
            product.tags.add('a_new_tag_1') //the tag is saved to the taggit db, but the association with the product isn't kept.
            product.save()
            self.save_m2m()
            return m

I tried to do the saving in the admin class instead, but this doesn't work either:

class ProductAdmin(admin.ModelAdmin):
    form = ProductForm
    def save_model(self, request, obj, form, change):
        obj.type="new_type" //this works 
        obj.tags.add("a_new_tag_2") //tag association not saved
        obj.save()
        form.save_m2m()

What am I doing wrong? Thanks in advance!

David Wolever
  • 148,955
  • 89
  • 346
  • 502
ruedaminute
  • 579
  • 2
  • 5
  • 13

2 Answers2

2

So it turns out that form.save_m2m() was the culprit. If I took it out of my own code, and commented it out in django.contrib.admin.options.py (line 983), the associations as well as the tag were saved.

Obviously it's not a good idea to change django's code, so I ended up overriding change_view() in my ProductAdmin (as well as add_view()). I added the tags after calling super(), so form.save_m2m() wouldn't overwrite my tag associations.

This is strange because it goes directly against django-taggit's documentation which emphasizes how important it is to call form.save_m2m() : http://django-taggit.readthedocs.org/en/latest/forms.html

Well I dunno what's up, I'll probably go on the taggit google groups and notify 'em. In any case thanks David for your help, if nothing less pdb is AWESOME and I did not know about it before :)

ruedaminute
  • 579
  • 2
  • 5
  • 13
0

Which tagging system are you using? Possibly you need to use product.tags.save()?

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • Hey David, I'm using django-taggit: https://github.com/alex/django-taggit... unforutnately TaggableManager doesnt have a save() method, so that can't be it... – ruedaminute May 11 '11 at 19:16
  • Hrm, ok. In that case, are you sure it's plugged in? Eg, is the `type` being updated the way you expect? Or if you use `pdb` to break after adding the tag, before saving, does the tag appear in the list? Does it appear after saving? – David Wolever May 11 '11 at 19:26
  • i didnt know about pdb, will def give it a look (still a python noob) but to answer your questions, type is being updated correctly, and when I do a print product.tags.all() before and after saving (but still within the ProductForm.save method) the new tag shows up correctly with the product's old tags in the console output. however when i do it outside, i.e. pull up a new shell or try to view it in the admin, the new tag doesn't show up. – ruedaminute May 11 '11 at 19:36
  • pdb is awesome. interestingly, Product.objects.filter(tags__name__in=["a_new_tag_1"]) spits out the associated product while I am in the code that's running above-- but when the code is finished running, the association hasn't saved! – ruedaminute May 11 '11 at 19:58
  • Are other changes being saved? Is it possible a database transaction is being rolled back or something? – David Wolever May 11 '11 at 20:08