Django Taggit - Le associazioni di tag non salvano dal modulo di amministrazione personalizzato
-
12-11-2019 - |
Domanda
Impazzire qui ... dall'interno del guscio, posso fare:
product.tags.add("a_new_tag")
Il tag viene aggiunto al DB e l'associazione del tag con il prodotto funziona correttamente. (cioè, quando lo faccio Product.objects.filter(tags__name__in=["a_new_tag"]
Il prodotto appropriato sputa)
Quello che devo fare è aggiungere alcuni tag nell'amministratore quando il modulo viene elaborato.
Ecco il mio codice di modulo (leggi i commenti nelle righe 4 e 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
Ho provato invece a fare il salvataggio nella classe di amministrazione, ma non funziona neanche:
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()
Che cosa sto facendo di sbagliato? Grazie in anticipo!
Nessuna soluzione corretta
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow