#36070: clean_fields(exclude=["pk"]) not implemented for composite primary keys
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                     Type:
                                     |  Cleanup/optimization
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  dev                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 Passing `exclude=["pk"]` to `full_clean()` / `clean_fields()` has no
 effect: the underlying fields have to be provided instead.

 This "no effect" might be silent (coercing a value when you didn't expect)
 or loud (raising ValidationError) given that we don't raise `ValueError`
 for providing an extraneous field name.

 I'm suggesting we either implement `exclude=["pk"]` to exclude all the
 composed fields or raise `ValueError`. Raising an error might be wiser?

 Failing test:
 {{{#!diff
 diff --git a/tests/composite_pk/test_models.py
 b/tests/composite_pk/test_models.py
 index ca6ad8b5dc..ed54d6c352 100644
 --- a/tests/composite_pk/test_models.py
 +++ b/tests/composite_pk/test_models.py
 @@ -118,6 +118,10 @@ class CompositePKModelsTests(TestCase):

                  self.assertSequenceEqual(ctx.exception.messages,
 messages)

 +    def test_clean_fields_exclude_pk(self):
 +        post = Token()
 +        post.clean_fields(exclude=["pk"])
 +
      def test_field_conflicts(self):
          test_cases = (
              ({"pk": (1, 1), "id": 2}, (1, 1)),
 }}}
 {{{
 ======================================================================
 ERROR: test_clean_fields_exclude_pk
 (composite_pk.test_models.CompositePKModelsTests.test_clean_fields_exclude_pk)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/Users/.../django/tests/composite_pk/test_models.py", line 123, in
 test_clean_fields_exclude_pk
     post.clean_fields(exclude=["pk"])
     ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/base.py", line 1708, in
 clean_fields
     raise ValidationError(errors)
 django.core.exceptions.ValidationError: {'tenant': ['This field cannot be
 null.'], 'id': ['This field cannot be null.']}

 ----------------------------------------------------------------------
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36070>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070194410bcca7-bff362b5-c374-4dc7-808c-9a67a792c4e9-000000%40eu-central-1.amazonses.com.

Reply via email to