Saturday, 8 January 2022

Access cleaned data from django admin for pdf processing

Hi there I'm fairly new to django so please be kind if anything sounds a bit newbie to you. I am currently trying to add a print button to every existing django admin view of a project so it will not depend on a single model I could resolve manually for printing but every existing model which contains foreign keys to other models an so on. For simplicity I thought it would be the best to just get the cleaned_data of the form and process it for the pdf.

I alread added the print button, the path url and so on and it will create a pdf file for me.

What I am not able to do is to access the forms cleaned_data from my BaseAdmins (extends the ModelAdmin) class like this:

form = BaseForm(request.POST)
if form.is_valid():
   data = form.cleaned_data 

It will just give me random kinds of errors, like object has no attribute 'is_bound' So I think that I am generally wrong with the context where I am trying to get the cleaned_data from the form. Every tutorial I found is just showing how to get the data but not fully resolved if it contains foreign keys and not in which context.

Could you please clear up for me where it would make sense to pass any kind of form data maybe as session data or post body to a print view where I can process it.

Thank you very much for reading, hope I was able to describe my problem, feel free to ask.

Edit This is the BaseForm I changed variable names for internal reasons:

class BaseForm(ModelForm):

    def clean_custom(self):
        another_model = self.cleaned_data.get('AnotherModel')
        custom_models = self.cleaned_data.get('CustomModel')
        custom_models_allowed = CustomModel.objects.filter(AnotherModel=another_model)
        custom_models_was_list = True

        if not custom_models:
            return

        if not isinstance(custom_models, Iterable):
            custom_models_was_list = False
            custom_models = [custom_models]

        for custom_model in custom_models:
            if another_model and custom_model not in custom_models_allowed:
                custom_models_allowed = [custom_model.titel for custom_model in custom_models_allowed]              
                custom_models_allowed = ', '.join(custom_models_allowed)
                raise ValidationError('{} is not part of {}. For selection: {}'.format(custom_model, another_model, custom_models_allowed))

        if custom_models_was_list:
            return custom_models
        else:
            return custom_models[0]

I'm trying to add cleaned_data in my BaseAdmin class where I have access to request and get the following trace:

AttributeError

AttributeError: type object 'CustomForm' has no attribute 'is_bound'
Traceback (most recent call last)

    File ".../.env/lib/python3.9/site-packages/django/contrib/staticfiles/handlers.py", line 65, in __call__

    return self.application(environ, start_response)

    File ".../.env/lib/python3.9/site-packages/django/core/handlers/wsgi.py", line 141, in __call__

    response = self.get_response(request)

    File ".../.env/lib/python3.9/site-packages/django/core/handlers/base.py", line 75, in get_response

    response = self._middleware_chain(request)

    File ".../.env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 36, in inner

    response = response_for_exception(request, exc)

    File ".../.env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception

    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

    File ".../.env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 125, in handle_uncaught_exception

    return debug.technical_500_response(request, *exc_info)

    File ".../.env/lib/python3.9/site-packages/django_extensions/management/technical_response.py", line 40, in null_technical_500_response

    raise exc_value.with_traceback(tb)

    File ".../.env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 34, in inner

    response = get_response(request)

    File ".../.env/lib/python3.9/site-packages/django/core/handlers/base.py", line 115, in _get_response

    response = self.process_exception_by_middleware(e, request)

    File ".../.env/lib/python3.9/site-packages/django/core/handlers/base.py", line 113, in _get_response

    response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File ".../.env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 606, in wrapper

    return self.admin_site.admin_view(view)(*args, **kwargs)

    File ".../.env/lib/python3.9/site-packages/django/utils/decorators.py", line 142, in _wrapped_view

    response = view_func(request, *args, **kwargs)

    File ".../.env/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func

    response = view_func(request, *args, **kwargs)

    File ".../.env/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 223, in inner

    return view(request, *args, **kwargs)

    File ".../admin/base_admin.py", line 203, in change_view

    if self.form.is_valid(self.form):

    File ".../.env/lib/python3.9/site-packages/django/forms/forms.py", line 185, in is_valid

    return self.is_bound and not self.errors

    AttributeError: type object 'CustomForm' has no attribute 'is_bound'




from Access cleaned data from django admin for pdf processing

No comments:

Post a Comment