|
13 | 13 |
|
14 | 14 |
|
15 | 15 | class CustomTestingField(models.TextField): |
16 | | - def get_db_prep_save(self, value, connection): |
| 16 | + def get_db_prep_value(self, value, connection, prepared=False): |
17 | 17 | """ |
18 | 18 | Note: If there is data migration when migrating to TranslatedField, the manually added |
19 | 19 | step_1_safe_encode_content() function in the migration file will base64 encode the value in the field |
20 | | - before get_db_prep_save is called. |
| 20 | + before get_db_prep_value is called. |
21 | 21 |
|
22 | 22 | For example: |
23 | 23 | old value = "this is a book" |
24 | 24 | new value = '{"en": "dGhpcyBpcyBhIGJvb2s="}' |
25 | 25 |
|
26 | | - The new value is then parse to the get_db_prep_save() function. |
27 | | - If custom get_db_prep_save() function is used you will need to make sure that the custom get_db_prep_save() function is |
| 26 | + The new value is then parse to the get_db_prep_value() function. |
| 27 | + If custom get_db_prep_value() function is used you will need to make sure that the custom get_db_prep_value() function is |
28 | 28 | not modifying the input value. |
29 | 29 |
|
30 | 30 | Some examples, |
31 | | - 1. if the custom get_db_prep_save() function append a fix str to all input value: |
| 31 | + 1. if the custom get_db_prep_value() function append a fix str to all input value: |
32 | 32 | input value = '{"en": "dGhpcyBpcyBhIGJvb2s="}' |
33 | 33 | return value = '{"en": "dGhpcyBpcyBhIGJvb2s="}1234567' |
34 | 34 | this would raise "django.db.utils.DataError: invalid input syntax for type json" because the return value |
35 | | - from the custom get_db_prep_save() function is not valid json |
| 35 | + from the custom get_db_prep_value() function is not valid json |
36 | 36 |
|
37 | | - 2. if the custom get_db_prep_save() function bleach certain substring (e.g dGhpcy) on the input value: |
| 37 | + 2. if the custom get_db_prep_value() function bleach certain substring (e.g dGhpcy) on the input value: |
38 | 38 | input value = '{"en": "dGhpcyBpcyBhIGJvb2s="}' |
39 | 39 | return value = '{"en": "BpcyBhIGJvb2s="}' |
40 | 40 | this would modify the base64 value and decoding the modfied base64 value would return unexpected result |
41 | 41 | """ |
42 | 42 | if value is None: |
43 | | - return super().get_db_prep_save(value, connection) |
| 43 | + return super().get_db_prep_value(value, connection, prepared) |
44 | 44 | bleached_value = value.replace(BLEACH_STR, RANDOM_STR) |
45 | | - return super().get_db_prep_save(bleached_value, connection) |
| 45 | + return super().get_db_prep_value(bleached_value, connection, prepared) |
46 | 46 |
|
47 | 47 |
|
48 | 48 | def validate_length(value): |
|
0 commit comments