Skip to content

add GeneratedField#332

Open
taraskuzyk wants to merge 1 commit intosbdchd:mainfrom
taraskuzyk:main
Open

add GeneratedField#332
taraskuzyk wants to merge 1 commit intosbdchd:mainfrom
taraskuzyk:main

Conversation

@taraskuzyk
Copy link
Copy Markdown

GeneratedField was added in Django 5.0. I am making this pull request as a draft as I'd like to find out how to contribute to this repository (would love to also document that in a CONTRIBUTING.md).

I based this on JSONField implementation and main changes were taken based on the __init__ in source:

def __init__(self, *, expression, output_field, db_persist=None, **kwargs):
        if kwargs.setdefault("editable", False):
            raise ValueError("GeneratedField cannot be editable.")
        if not kwargs.setdefault("blank", True):
            raise ValueError("GeneratedField must be blank.")
        if kwargs.get("default", NOT_PROVIDED) is not NOT_PROVIDED:
            raise ValueError("GeneratedField cannot have a default.")
        if kwargs.get("db_default", NOT_PROVIDED) is not NOT_PROVIDED:
            raise ValueError("GeneratedField cannot have a database default.")
        if db_persist not in (True, False):
            raise ValueError("GeneratedField.db_persist must be True or False.")

Copy link
Copy Markdown
Owner

@sbdchd sbdchd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah seems good, if you want to test it, you can add some code inside https://github.com/sbdchd/django-types/blob/main/tests/trout/models.py and the type checker will make sure it works with the types!

@taraskuzyk
Copy link
Copy Markdown
Author

Will do. Had an unrelated question - I'm using grep for searching across codebase, and the .ignore contains django-stubs. I can only see it being edited once before, is there a reason that file exists?

@sbdchd
Copy link
Copy Markdown
Owner

sbdchd commented Sep 17, 2025

Oh I think that's a remnant of how the project used to be structured, there was like a dupe folder that was symbolic linked to another folder so you'd get dupes in vscode without the .ignore file

kodiakhq bot pushed a commit that referenced this pull request Oct 18, 2025
See #332 for a previous PR related to this. I have made the following changes:

1. I have made sure to reflect the latest up-to-date signature of `Field.__init__()`, except those fields that are explicitly not supported by `GeneratedField`.
2. I have added tests in the `trout` test app.
3. I have typed the setter as `Never`, as it does not make sense to set values on these fields, as they are overwritten in the database. This might be the most controversial choice.
4. I have not mixed in `Combinable` in the setter because of 3.
5. Made the field available through `django.db.models.GeneratedField` in addition to `django.db.models.fields.generated.GeneratedField`.


Thanks!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants