1
我想在Django管理界面保存時自動添加一些項目到模型的ManyToMany字段。但是,調用model.many2manyfield.add()被悄悄失敗定製的django管理類可以將項目添加到模型的ManyToMany字段嗎?
我Django應用程序的models.py
我Django應用程序的admin.py
from models import Dashboard
...
class DashboardAdmin(GraphAdmin):
def save_model(self, request, dashboard, form, change):
dashboard.save()
graphite_util.sync_dashboard_graphs(dashboard)
...
admin.site.register(Dashboard, DashboardAdmin)
從graphite_util.py:
def sync_dashboard_graphs(dashboard):
print "Syncing %s " % dashboard
...
graph = None
try:
graph = Graph.objects.get(url=url)
except Graph.DoesNotExist:
graph = Graph()
graph.url = url
graph.save()
print "adding graph %s to dashboard %s" %(graph, dashboard)
dashboard.graphs.add(graph) #TODO: why does this fail silently?
是我想做什麼非法?如果是這樣,還有其他選擇嗎?
謝謝!