Opened 8 years ago

Closed 8 years ago

#27613 closed Bug (invalid)

BaseInlineFormSet is validating form for extra forms

Reported by: Sonu kumar Owned by: nobody
Component: Forms Version: 1.10
Severity: Normal Keywords: FormSet
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

class ExplanationFormSet(BaseInlineFormSet):
    def clean(self):
        super(ExplanationFormSet, self).clean()
        for form in self.forms:
            if not hasattr(form, 'cleaned_data'):
                continue
            data = form.cleaned_data
            if data.get('description') is None or data.get('description') == "":
                continue

            if data.get('user') == None:
                raise ValidationError("Explanation Author is required")

@python_2_unicode_compatible
class Explanation(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey(Question)
    description = models.TextField()
    creation = models.DateTimeField(auto_now_add=True)
    last_change = models.DateTimeField(auto_now_add=True)
    past_explanation = models.ForeignKey('self', on_delete=models.CASCADE,
                                         db_constraint=False, blank=True,
                                         default=-1, null=True)
    valid = models.BooleanField(default=False)

    def __str__(self):
        return str(self.description[:40])

class ExplanationInline(TabularInline):
    model = Explanation
    formset = ExplanationFormSet
    extra = 1

@register(Question)
class QuestionAdmin(ModelAdmin):
    inlines = [
        ItemInline,
        ExplanationInline,
    ]
    list_filter = ['multiple_correct', 'timestamp','field__field_name',
                   'field__subfield_name' ]
    exclude = ('timestamp',)
    date_hierarchy = 'timestamp'
    search_fields = ['header']

Problem:

When Question is edited in admin panel then it always throws an error for one additional form added at last. Form throws the error for all fields whose value(s) are required. This is especially seen for ExplanationFormSet.

Change History (5)

comment:1 by Tim Graham, 8 years ago

Easy pickings: unset
Severity: Release blockerNormal

Could you try to simplify your example code so that it only contains the minimum code needed to reproduce the issue as well as an explanation of why Django appears to be at fault?

comment:2 by Sonu kumar, 8 years ago

Hi Tim,
I have found root caused this is happen only when you add following code snippet to Explanation model.

past_explanation = models.ForeignKey('self', on_delete=models.CASCADE,
                                         db_constraint=False, blank=True,
                                         default=-1, null=True)

I think this is a bug since, from the model and admin configuration we can see that user should be able to add Question without any explanation, but it's not allowing to do so.

You can find code at GitHub repo https://github.com/sonus21/Django_Bug

comment:3 by Tim Graham, 8 years ago

Does it happen if you remove default=-1? That looks unusual. Can you explain the use case?

comment:4 by Sonu kumar, 8 years ago

Hmm, It's not happening if you remove default=-1, but do you know any reason for this?
.

comment:5 by Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed

The foreign key widget would need to send "-1" on a form submission so that the field doesn't appear to change. Since the field appears to change, Django validates the form.

You'll likely need to use a custom widget or a different method to accomplish your use case. I don't think it's a bug in Django, but if you want to propose some change that fixes the issue for you, feel free to reopen with a patch.

Note: See TracTickets for help on using tickets.
Back to Top