用于多个数据库的 Django GenericTabularInline

标签 django django-admin inline-editing inline-formset generic-foreign-key

我一直在尝试通过从 GenericTabularInline 类继承并覆盖 BaseModelAdmin 类中的一些方法,使 GenericTabularInline 类在双管理员双数据库设置中工作,就像在 Django 文档 (https://docs. djangoproject.com/en/dev/topics/db/multi-db/),但如果以内联形式编辑子模型,它总是写入默认数据库(我希望第二个管理员专门处理辅助数据库,两者的模型都相同),所以我不能重写某些方法或做错什么。这是我到目前为止的类(class):

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

感谢任何帮助或提示。

最佳答案

我意识到这是一个老问题,但我最近偶然发现了一个非常相似的问题。诀窍是覆盖父模型管理员的 save_formset 方法。就我而言,解决方案是执行以下操作:

class SomeTabularInline(admin.TabularInline):
    # stuff


class MyModelAdmin(admin.ModelAdmin):
    using = 'something'
    inlines = (SomeTabularInline,)

    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)
        for obj in formset.deleted_objects:
            obj.delete(using=self.using)
        for instance in instances:
            instance.save(using=self.using)
        formset.save_m2m()

注意:我使用的是 TabularInline 实例,而不是 GenericTabularInline,但它们都来自 InlineModelAdmin;所以我希望这对您有用。

来源:https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset

关于用于多个数据库的 Django GenericTabularInline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262727/

相关文章:

django - 在 django-admin 中更新记录的问题

javascript - 使用内联文本编辑更新数据库

django - 使用 Django South 向现有模型添加自动增量字段?

mysql - Django ORM : Tried to do inner join with foreign key but causes FieldError

python - Django - 管理工具中缺少一些权限

python - django admin 中不可修改的属性

bash - 如何在单行上重新运行 ZSH 中的内联循环(类似 bash 的行为)

json - jqgrid onClickSubmit 和内联保存按钮的 afterSubmit 事件

python - 如何在 Django 的新标签页中打开 url?

python - 使用 Django REST Framework 的 BitField 序列化