Opened 4 years ago

Last modified 9 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

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:

Attachments (1)

unique-together-formset.png (81.5 KB ) - added by Marco Beri 4 years ago.

Download all attachments as: .zip

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, 20 months ago

Owner: Aditya parashar removed
Status: assignednew

comment:7 by syed waheed, 10 months ago

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