add import and export functionality
This commit is contained in:
parent
cc5307a723
commit
f14cc0e39e
4 changed files with 68 additions and 2 deletions
|
@ -1,6 +1,10 @@
|
|||
from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
from adminsortable2.admin import SortableAdminMixin
|
||||
from import_export.admin import ImportExportModelAdmin
|
||||
from import_export import resources
|
||||
from import_export.fields import Field
|
||||
from import_export.widgets import ForeignKeyWidget
|
||||
|
||||
from .models import (
|
||||
Category,
|
||||
|
@ -188,8 +192,31 @@ class WebsiteFaqAdmin(SortableAdminMixin, admin.ModelAdmin):
|
|||
ordering = ("order",)
|
||||
|
||||
|
||||
class ComputePlanResource(resources.ModelResource):
|
||||
cloud_provider = Field(
|
||||
column_name="cloud_provider",
|
||||
attribute="cloud_provider",
|
||||
widget=ForeignKeyWidget(CloudProvider, "name"),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = ComputePlan
|
||||
skip_unchanged = True
|
||||
report_skipped = False
|
||||
import_id_fields = ["name"]
|
||||
fields = (
|
||||
"name",
|
||||
"vcpus",
|
||||
"ram",
|
||||
"cpu_mem_ratio",
|
||||
"price_chf",
|
||||
"cloud_provider",
|
||||
)
|
||||
|
||||
|
||||
@admin.register(ComputePlan)
|
||||
class ComputePlansAdmin(admin.ModelAdmin):
|
||||
class ComputePlansAdmin(ImportExportModelAdmin):
|
||||
resource_class = ComputePlanResource
|
||||
list_display = ("name", "cloud_provider", "vcpus", "ram", "price_chf", "active")
|
||||
search_fields = ("name", "cloud_provider")
|
||||
search_fields = ("name", "cloud_provider__name") # Search by cloud_provider name
|
||||
ordering = ("name",)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from pathlib import Path
|
||||
from environs import Env
|
||||
from import_export.formats.base_formats import CSV
|
||||
|
||||
env = Env()
|
||||
env.read_env()
|
||||
|
@ -79,6 +80,7 @@ INSTALLED_APPS = [
|
|||
"schema_viewer",
|
||||
"nested_admin",
|
||||
"adminsortable2",
|
||||
"import_export",
|
||||
# local
|
||||
"hub.services",
|
||||
"hub.broker",
|
||||
|
@ -245,3 +247,5 @@ JAZZMIN_SETTINGS = {
|
|||
"show_sidebar": True,
|
||||
"navigation_expanded": True,
|
||||
}
|
||||
|
||||
IMPORT_EXPORT_FORMATS = [CSV]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue