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

前端 未结 7 1100
佛祖请我去吃肉
佛祖请我去吃肉 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:54

    Interesting question! I'd like to see good and elegant solution for it :) But it seems to me, that django-admin is not suitable for your task. I'd try to play with Forms. Smth like this:

    class HmmForm(forms.Form):
        name = forms.CharField(max_length = 128)
        user_id = forms.IntegerField()
        height = forms.IntegerField()
        weight = forms.IntegerField()
    
    
    def test(request, pk):
        form = HmmForm()
        if pk > 0:
            hmm = Hmm.objects.get(pk = pk)
            form = HmmForm( initial = {"name": hmm.name} )
        return render_to_response("test/test.html", {"form": form})
    

    And then simple render form in template, as you wish:

    {{ form.as_table }} or {{ form.as_p }}
    

提交回复
热议问题