У меня есть эта модель:
Class Job(models.Model):
// Some fields
slug = models.SlugField(verbose_name=_("slug"), max_length=151, null=True, blank=True)
def get_absolute_url(self):
return reverse("jobs", kwargs={"slug": self.slug})
Slug должен принимать значение Null. И это так. Например, я создаю это на оболочке, и она работает нормально:
In [1]: j = Job.objects.create(title="my faked title",date_start=aware_start, date_end=aware_end, amount_to_pay=10, email='fake@gmail.com')
In [2]: j
Out[2]: <Job: my faked title>
Но если я хочу проверить это, на тестовом примере это не получается.
Контрольный пример:
class JobCreationTest(TestCase):
def test_simple_creation(self):
import datetime
import pytz
aware_start = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC)
aware_end = datetime.datetime(2012, 8, 15, 8, 15, 12, 0, pytz.UTC)
Job.objects.create(title="my faked title",date_start=aware_start, date_end=aware_end, amount_to_pay=10, email='fake@gmail.com')
И трассировка ошибки:
self = <django.db.backends.utils.CursorWrapper object at 0x7fc2bc72af28>
sql = 'INSERT INTO "posts_job" ("id", "created_at", "created_by_id", "updated_at", "updated_by_id", "deleted", "title", "ema...slug") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
params = (UUID('e814e054-e273-4316-b670-f39584f0b3ef'), datetime.datetime(2019, 3, 19, 9, 39, 31, 31514, tzinfo=<UTC>), None, datetime.datetime(2019, 3, 19, 9, 39, 31, 31566, tzinfo=<UTC>), None, False, ...)
ignored_wrapper_args = (False, {'connection': <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0x7fc2d0486f28>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7fc2bc72af28>})
def _execute(self, sql, params, *ignored_wrapper_args):
self.db.validate_no_broken_transaction()
with self.db.wrap_database_errors:
if params is None:
return self.cursor.execute(sql)
else:
> return self.cursor.execute(sql, params)
E django.db.utils.IntegrityError: null value in column "slug" violates not-null constraint
E DETAIL: Failing row contains (e814e054-e273-4316-b670-f39584f0b3ef, 2019-03-19 09:39:31.031514+00, null, 2019-03-19 09:39:31.031566+00, null, f, my faked title, fake@gmail.com, , null, null, null, null, null, null, null, null, 2011-08-15 08:15:12+00, 2012-08-15 08:15:12+00, null, null, null, , , 10, null).
UPDATE:
$ ./manage.py makemigrations posts
No changes detected in app 'posts'
$ ./manage.py migrate posts
Operations to perform:
Apply all migrations: posts
Running migrations:
No migrations to apply.
И в определении таблицы, в psql
> \d+ posts_job
Table "public.posts_job"
Column | Type | Collation | Nullable |
----------------------+--------------------------+-----------+----------+-
id | uuid | | not null |
title | character varying(128) | | not null |
email | character varying(254) | | not null |
slug | character varying(151) | | |