Django4.0 多數(shù)據(jù)庫-在Django管理界面中使用多數(shù)據(jù)庫

2022-03-16 17:38 更新

Django的管理后臺(tái)對多數(shù)據(jù)庫沒有明顯的支持。如果要為路由指定的數(shù)據(jù)庫以外的數(shù)據(jù)庫提供模型的管理界面,你需要編寫自定義的 ?ModelAdmin ?類,這個(gè)類將指示管理后臺(tái)使用指定數(shù)據(jù)庫的內(nèi)容。

?ModelAdmin ?對象有五種需要為多數(shù)據(jù)庫支持定制的方法:

class MultiDBModelAdmin(admin.ModelAdmin):
    # A handy constant for the name of the alternate database.
    using = 'other'

    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 get_queryset(self, request):
        # Tell Django to look for objects on the 'other' database.
        return super().get_queryset(request).using(self.using)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        # Tell Django to populate ForeignKey widgets using a query
        # on the 'other' database.
        return super().formfield_for_foreignkey(db_field, request, using=self.using, **kwargs)

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        # Tell Django to populate ManyToMany widgets using a query
        # on the 'other' database.
        return super().formfield_for_manytomany(db_field, request, using=self.using, **kwargs)

此處提供的實(shí)現(xiàn)方法實(shí)現(xiàn)了多數(shù)據(jù)庫策略,其中給定類型的所有對象保存在指定數(shù)據(jù)庫上(比如所有 ?User ?對象在 ?other ?數(shù)據(jù)庫中)。如果對多數(shù)據(jù)的使用很復(fù)雜,那么?ModelAdmin?將需要映射策略。
?InlineModelAdmin ?對象可以以類似的方式處理。它們需要三個(gè)自定義的方法:

class MultiDBTabularInline(admin.TabularInline):
    using = 'other'

    def get_queryset(self, request):
        # Tell Django to look for inline objects on the 'other' database.
        return super().get_queryset(request).using(self.using)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        # Tell Django to populate ForeignKey widgets using a query
        # on the 'other' database.
        return super().formfield_for_foreignkey(db_field, request, using=self.using, **kwargs)

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        # Tell Django to populate ManyToMany widgets using a query
        # on the 'other' database.
        return super().formfield_for_manytomany(db_field, request, using=self.using, **kwargs)

一旦編寫了模型管理定義,就可以在任何 ?Admin ?實(shí)例中注冊:

from django.contrib import admin

# Specialize the multi-db admin objects for use with specific models.
class BookInline(MultiDBTabularInline):
    model = Book

class PublisherAdmin(MultiDBModelAdmin):
    inlines = [BookInline]

admin.site.register(Author, MultiDBModelAdmin)
admin.site.register(Publisher, PublisherAdmin)

othersite = admin.AdminSite('othersite')
othersite.register(Publisher, MultiDBModelAdmin)

這個(gè)例子設(shè)置了兩個(gè)管理長點(diǎn)。在第一個(gè)站點(diǎn)上,?Author ?和 ?Publisher ?對象是顯式的;?Publisher ?對象有一個(gè)表格行來顯示出版者的書籍。第二個(gè)站點(diǎn)只顯示出版者,不顯示內(nèi)嵌。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)