Pseudo-form in Django admin that generates a json object on save

前端 未结 7 1084
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 18:07

I have a model with a field for a json object. This object is used on the site to control some css variables, among other things.

Right now in the admin, I have a t

7条回答
  •  攒了一身酷
    2020-12-23 18:38

    It's looks simple like this:

    #Creating custom form 
    class MyCoolForm(forms.ModelForm):
        class Meta: 
            model = MyModel
            exclude = ('field_that_stores_json', ) 
        #field_that_shows_json1 = forms.CharField() 
        #field_that_shows_jsons = forms.CharField() 
    
        def __init__(self, *args, **kwargs):
            #Deserizlize field that stores json here
    
        def save(self, *args, **kwargs):
            #Serialize fields that shows json here
    

    After all, just set this form as a form for admin.

    P.S.: Also you can write your own widget for form, that transforms json object into fields on js level and has textarea underneath.

提交回复
热议问题