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

Decoded differently than what we see in DB #19

Open
tiholic opened this issue Jul 27, 2021 · 2 comments
Open

Decoded differently than what we see in DB #19

tiholic opened this issue Jul 27, 2021 · 2 comments

Comments

@tiholic
Copy link
Contributor

tiholic commented Jul 27, 2021

Write Input DB Storage On Read (as relativedelta)
P2M1DT6H "2 mons 1 day 06:00:00" 61 days 6 hours
P1M "1 mon" 30 days
P3M "3 mons" 90 days
P1Y "1 year" 365 days

I was expecting this field to store and retrieve the data in same format. But, the current behaviour is not different from Duration field except that this has more sophisticated options for inputting data.

On some debugging, I noticed that django is reading the values as timedelta by default and then relativedelta field gets hook for transforming the read value.

my requirements.txt

Django==3.2.5
django-relativedelta==1.1.2
psycopg2-binary==2.9.1

PS: I can create a PR if you can suggest on a way how to fix this.

@tiholic
Copy link
Contributor Author

tiholic commented Jul 27, 2021

May be using a varchar backend and storing simple designator based format to that would be more appropriate? 🤔

Not sure how that affects filtering queries though!

@tiholic
Copy link
Contributor Author

tiholic commented Jul 27, 2021

Here's a fix I came up with. I created a TimeIntervalField extending RelativeDeltaField like this and always use ISO8601 format to store in DB

class TimeIntervalField(RelativeDeltaField):
    def db_type(self, connection):
        return 'varchar(33)'

    def value_from_object(self, obj):
        return obj

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