Error: dictionary changed size during iteration [closed]

I am trying to convert a model instance to a dictionary with the function "model_to_dict" from django.forms.models but im getting the error:
dictionary changed size during iteration.

The model_to_dict function is supposed to convert a model instance to a dictionary object, keeping all the fields intact.


    model_category = models.ForeignKey(ModelCategory, on_delete=models.CASCADE)
    initial_display = models.DecimalField(default=0.00, max_digits=19, decimal_places=2)
    final_display = models.DecimalField(default=0.00, max_digits=19, decimal_places=2)
    active_variables = models.ManyToManyField(Variable)

Code Snippet

obj_list = []
    object_qs = Model.objects.all()
    for obj in object_qs:
        obj_list.append(model_to_dict(obj, exclude=['active_variables']))

The desired result is a list of objects like
{ "model_category":"2",

I don’t understand what’s happening and how do i achieve the desired result?

Back to Top