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

About daily sequence #31

Closed
hc2021hc opened this issue Feb 21, 2022 · 7 comments
Closed

About daily sequence #31

hc2021hc opened this issue Feb 21, 2022 · 7 comments

Comments

@hc2021hc
Copy link

Hello, I would like to generate a sequence which renews everyday. Is there any function to do so?
e.g. yyyymmddNN. where NN will start from 01 for each date. Thanks.

@adamchainz
Copy link
Collaborator

adamchainz commented Feb 21, 2022

This should work:

from datetime import datetime, timezone
from sequences import get_next_value
get_next_value(f"books-{datetime.now(timezone.utc).date().isoformat()}")

Swap books for some other domain-related prefix. And think of timezones - this uses UTC which may not apply to your use case!

@hc2021hc
Copy link
Author

Sorry I'm new to Python. I would like to ask how I can fallback to previous value if some transaction fails?

say, with transaction.atomic():
get next seq value()
doing sth
save to database

For example, if get_next_value is 2 and save to database fails, I hope the seq can be reverted to 1, how can it be achieved?

@adamchainz
Copy link
Collaborator

That should already happen - as the README says:

if the transaction is rolled back, the generated value isn't consumed.

@spapas
Copy link
Contributor

spapas commented Mar 8, 2023

Hey, this is very useful, please put it in the README!

I need a sequence of the form number / year (which will reset each year) and it solves my problem just fine

@adamchainz
Copy link
Collaborator

Sure, we could do that, would you like to prepare a PR @spapas ?

@aaugustin
Copy link
Owner

Indeed, we could add an "Examples" section after "API" and before "Database support".

@spapas
Copy link
Contributor

spapas commented Mar 16, 2023

Hey friends, I've submitted PR #35 with the examples section.

Kind regards,
Serafeim

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

4 participants