0

How do I add an arbitrary hidden input to a specific model's view and populate it with a value? I would like to get it later from request.GET.get('my_input_name') on submit (e.g. "Save") . It's not a field that exist in the model, it's just a random value. Also: I am not trying to make some existing field hidden with widgets. If possible, without introducing a custom template.

My view is a model-based class in admin.py ( I have no forms.py ) with custom get_form and formfield_for_foreignkey methods.

I looked at several solutions related to Dynamic fields, they are not working in Django 2.2:

Dynamical Form fields in __init__in Django admin

Dynamic fields in Django Admin

Add dynamic field to django admin model form

I don't really care about it being a dynamic field, or widget or whatever, as long as I can have it saved and retrieved from the request on GET / POST submit. It's also somewhat strange that I don't find any discussions (or documentation) about dynamic fields for Django 2.2 or higher.

My code

model.py

class MyModel(SuperModel):
    ...model fields definitions...

admin.py

from .models import MyModel  
class MyModelAdmin(SuperAdmin):
    def get_form(self, request, obj=None, **kwargs):
       ...some code...


    def formfield_for_foreignkey(self, db_field, request, **kwargs):
       ...some code...
admin.site.register(MyModel, MyModelAdmin)

Thanks.

UPDATE

Never figured out how to implement what I wanted, so solved it with an ugly but working "flag-based" workaround using request.session. It does not answer the question so I do not post it as an answer.

I store in the session the value upon arrival to the entity edit form from the list (in get_form), use it in subsequent POST submits (formfield_for_foreignkey) and erase it when it's not needed anymore (get_queryset). This way I have one out of two states defined, i.e. everything is filtered down to the "parent id" passed from the "list" view to "edit" view, or not, and this state persists through multiple actions in "edit" view. Relevant code:

admin.py

class MyModelAdmin(SuperAdmin)

    def get_form(self, request, obj=None, **kwargs):
        ......
        if "parent_model_value_inline_request" in request.GET:
            request.session['parent_model_value'] = request.GET['parent_model_value_inline_request']
        ......

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        ......
        if db_field.name == "parent_model_dropdown_name":
            # Make sure we are dealing with a GET method
            if request.method == 'GET' or request.method == 'POST': # GET - arrived from  list, POST - submitting "save" or "save and and add another"

                parent_model_value = None

                # if the GET method contains the variable we are interested in that was passed by the action button
                # "add entity":
                if "parent_model_value_inline_request" in request.GET: # coming from parent with "add child" button
                    parent_model_value = request.GET.get('parent_model_value_inline_request')
                elif 'parent_model_value' in request.session:
                    if request.session['parent_model_value'] != None:
                        parent_model_value = request.session['parent_model_value']

                if parent_model_value:
                    ...do conditional stuff...

    def get_queryset(self, request):
        .........
        request.session['parent_model_value'] = None
        ........

UPDATE Jan 24 2020. No luck so far (tried a custom init gain in a different way), so I implemented it by writing needed info in session and erasing it later.

SwissNavy
  • 619
  • 1
  • 12
  • 28
  • 1
    how is your view being rendered right now? – rabbit.aaron Dec 31 '19 at 14:43
  • I updated the question with my view description – SwissNavy Dec 31 '19 at 15:32
  • Are you trying to implement something like the built-in `csrf_token`? Also please share your code instead of _plain_ description so that we have a better impact of what you're trying to accomplish. – Pedram Parsian Dec 31 '19 at 15:34
  • I think you would either need to do your own template or create a custom form... or at least modify the form in the `__init__` method in your admin. check this answer: https://stackoverflow.com/a/6788299/3452345 – rabbit.aaron Dec 31 '19 at 15:36
  • @PedramParsian Yes, exactly like `csrf_token` is handled. I added some code (the starting point, without any solutions) and links to other SO questions that did not work for me. – SwissNavy Jan 02 '20 at 12:04

0 Answers0