0

In my Django application, I am using bulk_create(). For one of the fields in a target model I have assigned a set of validators to restrict the allowed value to uppercase letters (alphabets) and to a fixed length of "3", as shown below:

class Plant(models.Model):
    plant = models.CharField(primary_key=True, max_length=4, ...
    plant_name = models.CharField(max_length=75, ...
    plant_short_name = models.CharField(max_length=3, validators=[...
    # rest of the fields ...

I am restricting field plant_short_name to something like CHT for say, Plant Charlotte.

Using the source file (.csv) I am able to successfully create new instances using bulk_create, however I find that the data get saved even with field plant_short_name's value being different.

For example, if I use the source as:

plant,plant_name,plant_short_name
9999,XYZ Plant,XY

the new instance still gets created although the length of (string) value of field plant_short_name is only 2 (instead of 3 as defined in the validators).

If I am to use an online create function (say, Django CreateView), the validators work as expected.

How do I control / rstrict the creation of model instance when a field value of incorrect length is used in the source file?

carla
  • 141
  • 9
  • I doubt validators work for bulk_create( am not sure), anyway model save method won't work for bulk_create. Put some logs in the custom validator and check whether its calling – Jisson Dec 22 '22 at 05:46
  • https://code.djangoproject.com/ticket/31384 – Jisson Dec 22 '22 at 05:47
  • @Jisson It does not. Anyway, I was going through Django docs trying to find out if I may capture the validators somehow (something like `_meta` values for model fields). Not sure though. Can you please come back on this? – carla Dec 22 '22 at 14:26

1 Answers1

0

bulk_create():

This method inserts the provided list of objects into the database in an efficient manner (generally only 1 query, no matter how many objects there are). Also, does not call save() on each of the instances, do not send any pre/post_save signals.

By efficient manner it means there is no validation. You can explore more of the function code in django/models/db/query.py inside the environment.

Niko
  • 3,012
  • 2
  • 8
  • 14
  • Thanks. In my query, I have pointed out that I am using `custom` validators for restricting the minimum length of the field to **three (3)**, that it should accept only letters, and that all the letters must be `uppercase` only (besides specifically restricting maximum lenght using `max_length=3`). And while creating new instance/s using Django CBV `CreateView`, the validators are working just fine. – carla Dec 21 '22 at 15:42
  • Sorry, I seem to have misinterpreted your question. As described in [docs](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#bulk-create), `bulk_create()`comes with a number of caveats, if its not validating properly it means that is creating every object without calling `.full_clean()` apparently. Which means that will also save `plant` field with more than four chars. – Niko Dec 21 '22 at 16:00