filters, list_editable, format json field
This commit is contained in:
parent
01c247fd42
commit
2775a952f2
3 changed files with 40 additions and 2 deletions
|
@ -1,10 +1,15 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from django.db.models import JSONField
|
||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
|
from ..widgets import PrettyJSONWidget
|
||||||
|
|
||||||
|
|
||||||
@admin.decorators.register(models.Text)
|
@admin.decorators.register(models.Text)
|
||||||
class TextAdmin(admin.ModelAdmin):
|
class TextAdmin(admin.ModelAdmin):
|
||||||
|
formfield_overrides = {
|
||||||
|
JSONField: {'widget': PrettyJSONWidget}
|
||||||
|
}
|
||||||
list_display = (
|
list_display = (
|
||||||
'__str__',
|
'__str__',
|
||||||
'item',
|
'item',
|
||||||
|
@ -14,7 +19,9 @@ class TextAdmin(admin.ModelAdmin):
|
||||||
'public',
|
'public',
|
||||||
'position',
|
'position',
|
||||||
)
|
)
|
||||||
list_editable = ['public', 'language']
|
list_editable = ['public', 'language', 'position']
|
||||||
|
list_filter = ['language', 'public']
|
||||||
|
|
||||||
def item(self, obj):
|
def item(self, obj):
|
||||||
return obj.data.get('item')
|
return obj.data.get('item')
|
||||||
def edit(self, obj):
|
def edit(self, obj):
|
||||||
|
|
|
@ -1,16 +1,26 @@
|
||||||
|
import logging
|
||||||
|
import json
|
||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from django.db.models import JSONField
|
||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
|
from ..widgets import PrettyJSONWidget
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@admin.decorators.register(models.Film)
|
@admin.decorators.register(models.Film)
|
||||||
class FilmAdmin(admin.ModelAdmin):
|
class FilmAdmin(admin.ModelAdmin):
|
||||||
|
formfield_overrides = {
|
||||||
|
JSONField: {'widget': PrettyJSONWidget}
|
||||||
|
}
|
||||||
list_display = (
|
list_display = (
|
||||||
'__str__',
|
'__str__',
|
||||||
'position',
|
'position',
|
||||||
'slug',
|
'slug',
|
||||||
'public',
|
'public',
|
||||||
)
|
)
|
||||||
|
list_editable = ['public', 'position']
|
||||||
|
list_filter = ['public']
|
||||||
|
|
||||||
#@admin.decorators.register(models.Edit)
|
#@admin.decorators.register(models.Edit)
|
||||||
#class EditAdmin(admin.ModelAdmin):
|
#class EditAdmin(admin.ModelAdmin):
|
||||||
|
|
21
app/widgets.py
Normal file
21
app/widgets.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import logging
|
||||||
|
import json
|
||||||
|
|
||||||
|
from django.forms import widgets
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
class PrettyJSONWidget(widgets.Textarea):
|
||||||
|
|
||||||
|
def format_value(self, value):
|
||||||
|
try:
|
||||||
|
value = json.dumps(json.loads(value), indent=2, sort_keys=True, ensure_ascii=False)
|
||||||
|
# these lines will try to adjust size of TextArea to fit to content
|
||||||
|
row_lengths = [len(r) for r in value.split('\n')]
|
||||||
|
self.attrs['rows'] = min(max(len(row_lengths) + 2, 10), 30)
|
||||||
|
self.attrs['cols'] = min(max(max(row_lengths) + 2, 40), 120)
|
||||||
|
return value
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning("Error while formatting JSON: {}".format(e))
|
||||||
|
return super(PrettyJSONWidget, self).format_value(value)
|
||||||
|
|
Loading…
Reference in a new issue