Skip to content

FormCollection self.instance bug due to FormCollection.full_clean() #159

@codematsing

Description

@codematsing

Given

#models.py
class ParentModel(self):
    name = models.CharField
    
class ChildModel(self):
    parent = models.ForeignKey(Parent, related_name='children', on_delete=models.CASCADE)
    name = models.CharField

#forms.py
class ChildForm(ModelForm):
  class Meta:
    model = ChildModel()
    fields = '__all__'

class ParentChildFormCollection(FormCollection):
    child_formset = ChildForm()

class UpdateView(SingleObjectMixin, FormCollectionView):
    model = ParentModel
    collection_class = ParentChildFormCollection()

    def get_initial():
      parent_object = self.get_object()
      initial = [
        {"child_form": {child.name for child in parent.children.all()}}
      ]
      return initial

Expectation:

#forms.py
class ParentChildFormCollection():
  def save(self):
    logger.info(self.instance.name)
    logger.info(type(self.instance))
    ...
  • Expectation self.instance.name should consistently be the parent_object.name passed in FormCollectionView.get_form_kwargs()

Actual Results:

  • self.instance.name would refer to the last object child.name after FormCollection.full_clean()

Observations:

# in class FormCollection
    def full_clean(self):
       ...
        logger.info(instance) #print parent_name
        if holder.is_valid():
            valid_holders[name] = holder
            logger.info(instance) #prints_child_name
      ...

Note:
I'll to replicate this in django-formset using the repo. I'll get back to you on this as I'm swamped at the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions