gpt4 book ai didi

python - Django Admin 不显示预先存在的对象

转载 作者:太空宇宙 更新时间:2023-11-03 15:10:30 25 4
gpt4 key购买 nike

我创建了一个类,用于将 Django-Admin 公开给多个数据库,如 Django 文档中所述。

我的 admin.py 看起来像这样

from django.contrib import admin

# Register your models here.
from .models import DestinationMap


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

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(MultiDBModelAdmin, 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(MultiDBModelAdmin, 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(MultiDBModelAdmin, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)



admin.site.register(DestinationMap, MultiDBModelAdmin)


#admin.site.register(DestinationMap)

管理界面显示模型名称,但有 0 个对象。

该表包含275个对象,我已确认使用sql。

到目前为止我找不到任何解决方案。如有帮助,将不胜感激。

最佳答案

您需要添加get_queryset函数

from django.contrib import admin

# Register your models here.
from .models import DestinationMap


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

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(MultiDBModelAdmin, self).get_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(MultiDBModelAdmin, 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(MultiDBModelAdmin, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)



admin.site.register(DestinationMap, MultiDBModelAdmin)

关于python - Django Admin 不显示预先存在的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44236804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com