Opened 4 years ago

Last modified 12 months ago

#31932 assigned New feature

Unique checking in formsets should exclude forms marked for deletion.

Reported by: Marco Beri Owned by: syed waheed
Component: Forms Version: 3.1
Severity: Normal Keywords: formset, inline, unique_together
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no
Pull Requests:17822 build:success

Description

Validation of a formset with a unique_together doesn't take deleted children into account.

Method is_valid of forms/formset.py::BaseFormSet should take in account deleted children forms to prevent a wrong validation error.

Simple models.py:

from django.db import models
from django.utils.translation import ugettext as _


class Book(models.Model):
    title = models.CharField(max_length=64, verbose_name=_("Title"))


class Language(models.Model):
    language = models.CharField(max_length=64, verbose_name=_("Language"))


class Translator(models.Model):
    name = models.CharField(max_length=64, verbose_name=_("Name"))
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    language = models.ForeignKey(Language, on_delete=models.CASCADE)

    class Meta:
        unique_together = ('book', 'language')

Simple admin.py:

from django.contrib import admin

from .models import Book, Language, Translator


class TranslatorInline(admin.TabularInline):
    model = Translator


@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    inlines = [TranslatorInline]


@admin.register(Language)
class LanguageAdmin(admin.ModelAdmin):
    pass

Wrong validation error:

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (9)

by Marco Beri, 4 years ago

Attachment: unique-together-formset.png added

comment:1 by Marco Beri, 4 years ago

Summary: unique_together with a formset prompt a wrong validationunique_together with a formset prompts a wrong validation

comment:2 by anujpandey785, 4 years ago

Owner: changed from nobody to anujpandey785
Status: newassigned

i would like to work on this.

comment:3 by anujpandey785, 4 years ago

Owner: anujpandey785 removed
Status: assignednew

comment:4 by Mariusz Felisiak, 4 years ago

Summary: unique_together with a formset prompts a wrong validationUnique checking in formsets should exclude forms marked for deletion.
Triage Stage: UnreviewedAccepted
Type: BugNew feature

Thanks for this ticket, this looks simpler then swapping unique values (#25139) but it's also tricky because unique checking exists at the database level. Tentatively accepted. Would you like to work on PoC?

comment:5 by Aditya parashar, 4 years ago

Owner: set to Aditya parashar
Status: newassigned

I think the solution may be use a clean method

comment:6 by Mariusz Felisiak, 2 years ago

Owner: Aditya parashar removed
Status: assignednew

comment:7 by syed waheed, 13 months ago

Owner: set to syed waheed
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top