django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

[5.0] Add `db_default=` parameter to models `Field` classes

Open Skorpyon opened this issue 1 year ago • 5 comments

Django 5 feature update.

Database-computed default values - db_default arg and attr on model Field.

Tests not written, looks like it haven't things to test.

Full Django 5 updates list.

Skorpyon avatar Dec 14 '23 19:12 Skorpyon

Tests not written, looks like it haven't things to test.

Yeah, don't worry about tests for .pyi files. Unless it's a particularly complicated case of decorators or generics.

intgr avatar Dec 14 '23 20:12 intgr

@intgr may you advice, please?

Python 3.12.0 (main, Nov 17 2023, 17:15:54) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print("\n".join(sys.path))

/Users/anton/.pyenv/versions/3.12.0/lib/python312.zip
/Users/anton/.pyenv/versions/3.12.0/lib/python3.12
/Users/anton/.pyenv/versions/3.12.0/lib/python3.12/lib-dynload
/Users/anton/Documents/projects/github/django-stubs/.venv/lib/python3.12/site-packages
/Users/anton/Documents/projects/github/django-stubs
/Users/anton/Documents/projects/github/django-stubs/ext

But bash ./scripts/stubtest.sh gives:

error: django failed to find stubs
error: django.apps failed to find stubs
error: django.apps.config failed to find stubs
...
note: unused allowlist entry django.views.static.DEFAULT_DIRECTORY_INDEX_TEMPLATE
note: unused allowlist entry django.views.static.builtin_template_path
Found 3010 errors (checked 888 modules)

I stuck, django-stubs installed:

> pip freeze
...
distlib==0.3.8
Django==5.0
-e git+ssh://[email protected]/Skorpyon/django-stubs.git@2cf7a510b57c47d2f56ece659cf4796e0f5fb8f3#egg=django_stubs
-e git+ssh://[email protected]/Skorpyon/django-stubs.git@2cf7a510b57c47d2f56ece659cf4796e0f5fb8f3#egg=django_stubs_ext&subdirectory=ext
filelock==3.13.1
...

Skorpyon avatar Dec 14 '23 22:12 Skorpyon

But bash ./scripts/stubtest.sh gives: Found 3010 errors (checked 888 modules)

Try removing .mypy_cache before running stubtest. There seems to be a bug, but I haven't bothered to debug/report it yet.

intgr avatar Dec 15 '23 00:12 intgr

Here we go... @intgr may you check files.pyi, please, not sure that it is correct. FileField haven't _ST generic in declaration.

Skorpyon avatar Dec 15 '23 11:12 Skorpyon

I remove db_default from ManyToManyField.__init__() args at all.

Technically it may be passed to init, but latest Django codebase written so ugly, without any forward thinking. That feature should be realized using some thing like DbDefaultMixin, adding db_default behaviour to specific fields only, but in Django it realized on top level, added to top parent class Field, so db_default available for all field subclasses, no matter have it sense or no.

If db_default have been set in ManyToManyField Django producing pretty fun SQL.

class DBDefaultModel(models.Model):
    created = models.DateField(db_default=Now())
    circumference = models.FloatField(db_default=2 * Pi())
    default_value = models.CharField(max_length=10, db_default=None)
    manyfield = models.ManyToManyField(BlogPost, db_default=123)
>>> from server.apps.main.models import DBDefaultModel
>>> mm = DBDefaultModel()
>>> mm.save()
INSERT INTO "main_dbdefaultmodel" ("created",  "circumference",  "default_value")
VALUES (DEFAULT, DEFAULT, DEFAULT)
RETURNING 
    "main_dbdefaultmodel"."id",
    "main_dbdefaultmodel"."created",
    "main_dbdefaultmodel"."circumference",
    "main_dbdefaultmodel"."default_value",
    "main_dbdefaultmodel"."manyfield";
django.db.utils.ProgrammingError: column main_dbdefaultmodel.manyfield does not exist
СТРОКА 1: ...mference", "main_dbdefaultmodel"."default_value", "main_dbde...

So Django trying retrieve .manyfield column value, thats not exists for sure, because it is many-to-many descriptor with intermediate table.

Skorpyon avatar Dec 16 '23 14:12 Skorpyon