'Django model queryset changed into json serializable not working

I have model objects saved in session as below from context-processors.

students = Student.objects.all()
studente = serializers.serialize('json', students)
request.session['students'] = students

In my model Forms I have

class NewIssueForm(forms.ModelForm):
    def __init__(self,*arg,students, **kwargs):
        super(NewIssueForm, self).__init__(*args, **kwargs)

        self.fields['borrower_id'] = students

I get an error when I try to run.

'''''
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 994, in render
    return render_value_in_context(output, context)
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 973, in render_value_in_context
    value = str(value)
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\html.py", line 376, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\forms\forms.py", line 132, in __str__
    return self.as_table()
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\forms\forms.py", line 270, in as_table
    return self._html_output(
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\forms\forms.py", line 198, in _html_output
    bf = self[name]
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\forms\forms.py", line 163, in __getitem__
    self._bound_fields_cache[name] = field.get_bound_field(self, name)
AttributeError: 'str' object has no attribute 'get_bound_field'

How can I make this field populate the students itemswithout the error?



Solution 1:[1]

Try

self.fields['borrower_id'].queryset = students

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergey Pugach