我一直試圖通過繼承它並重寫BaseModelAdmin類中的一些方法來使GenericTabularInline類在雙管理雙數據庫設置中工作,就像在Django文檔(https ://docs.djangoproject.com/en/dev/topics/db/multi-db/),但是如果一個子模型以內聯形式編輯,它總是寫入默認數據庫(我希望第二個管理員處理專門用於輔助數據庫,兩者的模型是相同的),所以我不能重寫某些方法或做錯某些事情。這裏是我到目前爲止的班級:多個數據庫的Django GenericTabularInline
class MultiDBGenericTabularInline(generic.GenericTabularInline):
using = settings.SECONDARY_DATABASE
def save_model(self, request, obj, form, change):
# Tell Django to save objects to the 'other' database.
obj.save(using=self.using)
def delete_model(self, request, obj):
# Tell Django to delete objects from the 'other' database
obj.delete(using=self.using)
def queryset(self, request):
# Tell Django to look for objects on the 'other' database.
return super(MultiDBGenericTabularInline, self).queryset(request).using(self.using)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
# Tell Django to populate ForeignKey widgets using a query
# on the 'other' database.
return super(MultiDBGenericTabularInline, self).formfield_for_foreignkey(db_field, request=request, using=self.using, **kwargs)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
# Tell Django to populate ManyToMany widgets using a query
# on the 'other' database.
return super(MultiDBGenericTabularInline, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)
#Override these three methods; otherwise the log manager attempts
#to write to the main db and raises an exception.
def log_addition(self, request, object):
pass
def log_change(self, request, object, message):
pass
def log_deletion(self, request, object, object_repr):
pass
任何幫助或暗示讚賞。