Ticket #25910: django-model-ctor-prop.diff

File django-model-ctor-prop.diff, 1.4 KB (added by Joshua Phillips, 8 years ago)

Patch to fix the problem, now with test

  • django/db/models/base.py

    commit 6454ba57324065129588d4a1a9485c7ca20c52e2
    Author: Joshua Phillips <jphillips@imap.cc>
    Date:   Thu Jan 21 16:49:40 2016 +0000
    
        Don't accept read-only property names in model constructors
    
    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index 028367d..333bee0 100644
    a b class Model(six.with_metaclass(ModelBase)):  
    435435            for prop in list(kwargs):
    436436                try:
    437437                    if isinstance(getattr(self.__class__, prop), property):
    438                         setattr(self, prop, kwargs.pop(prop))
     438                        setattr(self, prop, kwargs[prop])
     439                        del kwargs[prop]
    439440                except AttributeError:
    440441                    pass
    441442            if kwargs:
  • tests/properties/tests.py

    diff --git a/tests/properties/tests.py b/tests/properties/tests.py
    index 4544302..c998c15 100644
    a b class PropertyTests(TestCase):  
    1818        # The "full_name" property hasn't provided a "set" method.
    1919        self.assertRaises(AttributeError, setattr, self.a, 'full_name', 'Paul McCartney')
    2020
     21        # And cannot be used to initialize the class.
     22        self.assertRaises(TypeError, Person, full_name='Paul McCartney')
     23
    2124        # But "full_name_2" has, and it can be used to initialize the class.
    2225        a2 = Person(full_name_2='Paul McCartney')
    2326        a2.save()
Back to Top