Django admin + autocomplete_light 用于具有大数据集的多对多字段
Django admin + autocomplete_light for many-to-many field with large data-set
Django 版本:1.7
autocomplete_light 版本:2.0
我有一个多对多关系:订阅 <-> 应用,其中每个应用可以属于多个订阅,并且每个订阅可以包含多个应用程序。
预计订阅数量将达到数百,而应用数量约为 150 万。
使用 Django Admin 管理订阅及其应用程序会很棒,并且按照 Whosebug 上的建议使用 autocomplete_light 处理大型数据集。
下面是我想出的代码,但是它试图一次加载 AppMetadata 的所有 150 万条记录并挂起浏览器。
请指教。
# model.py
class AppMetadata(models.Model):
package_key = models.IntegerField(primary_key=True)
title = models.CharField(max_length=500)
package = models.CharField(max_length=400)
class Subscription(models.Model):
name = models.CharField(max_length=256, unique=True)
is_active = models.BooleanField('active', default=True)
class SubscriptionApp(models.Model):
subscription_key = models.ForeignKey(to=Subscription, on_delete=models.DO_NOTHING)
package_key = models.ManyToManyField(to=AppMetadata, db_column='package_key')
# autocomplete_light_registry.py
class AppMetadataAutocomplete(autocomplete_light.AutocompleteModelBase):
model = AppMetadata
search_fields = ('title', 'package')
choices = AppMetadata.objects.all()
limit_choices = 20
autocomplete_light.register(AppMetadataAutocomplete)
class SubscriptionAppAutocomplete(autocomplete_light.AutocompleteGenericBase):
model = SubscriptionApp
choices = (
Subscription.objects.all(),
AppMetadata.objects.all(),
)
search_fields = (
('^name', ),
('title', 'package'),
)
limit_choices = 20
autocomplete_light.register(SubscriptionAppAutocomplete)
class SubscriptionAutocomplete(autocomplete_light.AutocompleteModelBase):
model = Subscription
choices = Subscription.objects.all()
search_fields = ('name', )
limit_choices = 20
autocomplete_light.register(SubscriptionAutocomplete)
# admin.py
class SubscriptionAppInline(admin.TabularInline):
model = SubscriptionApp
class SubscriptionAdmin(admin.ModelAdmin):
form = autocomplete_light.modelform_factory(model=Subscription)
inlines = [SubscriptionAppInline]
admin.site.register(Subscription, SubscriptionAdmin)
# settings.py
INSTALLED_APPS = (
'autocomplete_light',
'django.contrib.admin',
...
...
# urls.py
urlpatterns = patterns(
...
# last record
url(r'^autocomplete/', include('autocomplete_light.urls')),
)
我想出了一个折衷方案。从上面的代码中,我删除了 SubscriptionAutocomplete 并重写了 admin.py 类 如下:
@admin.register(SubscriptionApp)
class SubscriptionAppAdmin(admin.ModelAdmin):
form = autocomplete_light.modelform_factory(model=SubscriptionApp)
class SubscriptionAppInline(admin.TabularInline):
model = SubscriptionApp
@admin.register(Subscription)
class SubscriptionAdmin(admin.ModelAdmin):
model = Subscription
ordering = ('name',)
Django 版本:1.7
autocomplete_light 版本:2.0
我有一个多对多关系:订阅 <-> 应用,其中每个应用可以属于多个订阅,并且每个订阅可以包含多个应用程序。 预计订阅数量将达到数百,而应用数量约为 150 万。
使用 Django Admin 管理订阅及其应用程序会很棒,并且按照 Whosebug 上的建议使用 autocomplete_light 处理大型数据集。 下面是我想出的代码,但是它试图一次加载 AppMetadata 的所有 150 万条记录并挂起浏览器。 请指教。
# model.py
class AppMetadata(models.Model):
package_key = models.IntegerField(primary_key=True)
title = models.CharField(max_length=500)
package = models.CharField(max_length=400)
class Subscription(models.Model):
name = models.CharField(max_length=256, unique=True)
is_active = models.BooleanField('active', default=True)
class SubscriptionApp(models.Model):
subscription_key = models.ForeignKey(to=Subscription, on_delete=models.DO_NOTHING)
package_key = models.ManyToManyField(to=AppMetadata, db_column='package_key')
# autocomplete_light_registry.py
class AppMetadataAutocomplete(autocomplete_light.AutocompleteModelBase):
model = AppMetadata
search_fields = ('title', 'package')
choices = AppMetadata.objects.all()
limit_choices = 20
autocomplete_light.register(AppMetadataAutocomplete)
class SubscriptionAppAutocomplete(autocomplete_light.AutocompleteGenericBase):
model = SubscriptionApp
choices = (
Subscription.objects.all(),
AppMetadata.objects.all(),
)
search_fields = (
('^name', ),
('title', 'package'),
)
limit_choices = 20
autocomplete_light.register(SubscriptionAppAutocomplete)
class SubscriptionAutocomplete(autocomplete_light.AutocompleteModelBase):
model = Subscription
choices = Subscription.objects.all()
search_fields = ('name', )
limit_choices = 20
autocomplete_light.register(SubscriptionAutocomplete)
# admin.py
class SubscriptionAppInline(admin.TabularInline):
model = SubscriptionApp
class SubscriptionAdmin(admin.ModelAdmin):
form = autocomplete_light.modelform_factory(model=Subscription)
inlines = [SubscriptionAppInline]
admin.site.register(Subscription, SubscriptionAdmin)
# settings.py
INSTALLED_APPS = (
'autocomplete_light',
'django.contrib.admin',
...
...
# urls.py
urlpatterns = patterns(
...
# last record
url(r'^autocomplete/', include('autocomplete_light.urls')),
)
我想出了一个折衷方案。从上面的代码中,我删除了 SubscriptionAutocomplete 并重写了 admin.py 类 如下:
@admin.register(SubscriptionApp)
class SubscriptionAppAdmin(admin.ModelAdmin):
form = autocomplete_light.modelform_factory(model=SubscriptionApp)
class SubscriptionAppInline(admin.TabularInline):
model = SubscriptionApp
@admin.register(Subscription)
class SubscriptionAdmin(admin.ModelAdmin):
model = Subscription
ordering = ('name',)