Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE REQUEST: Allow optional fields #78

Closed
HarrySky opened this issue Oct 4, 2020 · 2 comments
Closed

FEATURE REQUEST: Allow optional fields #78

HarrySky opened this issue Oct 4, 2020 · 2 comments

Comments

@HarrySky
Copy link

HarrySky commented Oct 4, 2020

Hello, thanks for the good library, is it possible to allow optional fields in Model when using Model.objects.create() (just don't pass fields to INSERT INTO)? Can work on this feature.

My use case is this: I have a table that automatically updates/sets some fields (ex. dt_created, dt_changed) and I would like to not pass datetime.now every time I need to insert a new row. Table SQL is below (PostgreSQL):

CREATE TABLE tokens (
    id               bigserial NOT NULL,
    user_id          text NOT NULL CHECK(trim(user_id) != ''),
    scope            text NOT NULL,
    access           text NOT NULL CHECK(trim(access) != ''),
    refresh          text NOT NULL CHECK(trim(refresh) != ''),
    expires_at       bigint NOT NULL,
    dt_created       timestamp with time zone NOT NULL DEFAULT NOW(),
    dt_changed       timestamp with time zone NOT NULL DEFAULT NOW(),
    dt_deleted       timestamp with time zone NULL,
    PRIMARY KEY (id)
);

Right now I don't even add dt_* fields to model, but I would like to have access to them without the need to specify dt_created/dt_changed when calling Model.objects.create()

Model right now:

class Token(Model):
    __tablename__ = "tokens"
    __database__ = database
    __metadata__ = metadata

    id = Integer(primary_key=True)
    user_id = Text()
    scope = Text(allow_blank=True)
    access = Text()
    refresh = Text()
    expires_at = Integer()

Model I would like to use:

class Token(Model):
    __tablename__ = "tokens"
    __database__ = database
    __metadata__ = metadata

    id = Integer(primary_key=True)
    user_id = Text()
    scope = Text(allow_blank=True)
    access = Text()
    refresh = Text()
    expires_at = Integer()
    dt_created = DateTime(optional=True)
    dt_created = DateTime(optional=True)
    dt_deleted = DateTime(allow_null=True, optional=True)
@HarrySky
Copy link
Author

HarrySky commented Oct 4, 2020

I guess we can add check for optional flag here:

required = [key for key, value in fields.items() if not value.has_default()]

@HarrySky
Copy link
Author

HarrySky commented Oct 7, 2020

I saw that read_only flag was used in #64 (right now you can't provide it directly into DateTime like DateTime(read_only=True) on that branch), but maybe if it will be possible - problem will be solved.

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

No branches or pull requests

1 participant