[Django] #30080: New optional settings - disable bulk_create in all managers and call self.full_clean() before saving the models

13 views
Skip to first unread message

Django

unread,
Jan 5, 2019, 4:13:55 PM1/5/19
to django-...@googlegroups.com
#30080: New optional settings - disable bulk_create in all managers and call
self.full_clean() before saving the models
-----------------------------------------+------------------------
Reporter: uri-rodberg | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 2.1
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 |
-----------------------------------------+------------------------
We are using Django for [https://github.com/speedy-net/speedy-net Speedy
Net and Speedy Match] (currently Django 1.11.18, we can't upgrade to a
newer version of Django because of one of our requirements, django-
modeltranslation). I want to validate each model before saving it to the
database, so I'm using `class ValidateModelMixin` as the base of each
model.

For this reason, I want to disable `bulk_create` in all managers in all of
our models. So I added this code:

{{{#!python
class ValidateModelMixin(object):
def save(self, *args, **kwargs):
"""Call `full_clean` before saving."""
self.full_clean()
return super().save(*args, **kwargs)


class ManagerMixin(object):
def bulk_create(self, *args, **kwargs):
raise NotImplementedError("bulk_create is not implemented.")


class BaseModel(ValidateModelMixin, models.Model):
def save(self, *args, **kwargs):
try:
field = self._meta.get_field('id')
if ((not (self.id)) and (hasattr(field, 'id_generator'))):
self.id = field.id_generator()
while
(self._meta.model.objects.filter(id=self.id).exists()):
self.id = field.id_generator()
except FieldDoesNotExist:
pass
return super().save(*args, **kwargs)

class Meta:
abstract = True


class TimeStampedModel(BaseModel):
date_created = models.DateTimeField(auto_now_add=True, db_index=True)
date_updated = models.DateTimeField(auto_now=True, db_index=True)

class Meta:
abstract = True


class BaseManager(ManagerMixin, models.Manager):
pass


class BaseUserManager(ManagerMixin, DjangoBaseUserManager):
pass


}}}

I thought maybe it's good to add these settings - disable `bulk_create` in
all managers and call `self.full_clean()` before saving the models - as an
optional settings both in the project and also in each model (maybe in
`class Meta`) so it will be possible to override Django's default both
per-project and also for any specific model. I understand that the default
is not to call `self.full_clean()` before saving the models and to allow
`bulk_create` in the managers, but I suspect this may lead to invalid data
in the database of the projects.

The current code in the master is on https://github.com/speedy-net/speedy-
net/blob/master/speedy/core/base/models.py, and the code in the branch I'm
currently working on is on https://github.com/speedy-net/speedy-
net/blob/uri_merge_with_master_2019-01-05_a/speedy/core/base/models.py.

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

Django

unread,
Jan 6, 2019, 12:40:36 AM1/6/19
to django-...@googlegroups.com
#30080: New optional settings - disable bulk_create in all managers and call
self.full_clean() before saving the models
-------------------------------+--------------------------------------

Reporter: uri-rodberg | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by uri-rodberg):

* version: 2.1 => 1.11


--
Ticket URL: <https://code.djangoproject.com/ticket/30080#comment:1>

Django

unread,
Jan 8, 2019, 9:08:20 AM1/8/19
to django-...@googlegroups.com
#30080: New optional settings - disable bulk_create in all managers and call
self.full_clean() before saving the models
-------------------------------------+-------------------------------------
Reporter: אורי | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix
* component: Uncategorized => Database layer (models, ORM)


Comment:

[https://groups.google.com/d/topic/django-
developers/nv7TldZ_qQI/discussion django-developers discussion]. Aymeric
suggested that these feature requests are too niche and I agree.

In the future, please limit tickets to one idea.

--
Ticket URL: <https://code.djangoproject.com/ticket/30080#comment:2>

Django

unread,
Jan 12, 2019, 9:00:43 AM1/12/19
to django-...@googlegroups.com
#30080: New optional settings - disable bulk_create in all managers and call
self.full_clean() before saving the models
-------------------------------------+-------------------------------------
Reporter: אורי | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by אורי:

Old description:

> class BaseManager(ManagerMixin, models.Manager):
> pass
>

> class BaseUserManager(ManagerMixin, DjangoBaseUserManager):
> pass
>

> /speedy-net/blob/master/speedy/core/base/models.py, and the code in the


> branch I'm currently working on is on https://github.com/speedy-net
> /speedy-
> net/blob/uri_merge_with_master_2019-01-05_a/speedy/core/base/models.py.

New description:


class BaseManager(ManagerMixin, models.Manager):
pass


class BaseUserManager(ManagerMixin, DjangoBaseUserManager):
pass


}}}

net/blob/staging/speedy/core/base/models.py.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30080#comment:3>

Reply all
Reply to author
Forward
0 new messages