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

Problem with durationfield v2.0.3 #3

Open
fuxb opened this issue Aug 17, 2016 · 2 comments
Open

Problem with durationfield v2.0.3 #3

fuxb opened this issue Aug 17, 2016 · 2 comments
Assignees

Comments

@fuxb
Copy link

fuxb commented Aug 17, 2016

Hi,

I just installed thecut-durationfield with pip, getting version 2.0.3.
The durationfield is a great idea since django doesn't support postgres
relative timedelta like 'mons' or 'year'. Thanks a lot for making this!

Unfortunately I have problems using the RelativeDeltaField, using a
model like documented on
https://pypi.python.org/pypi/thecut-durationfield/2.0.3:

class MyModel(models.Model):
    duration = RelativeDeltaField(blank=True, null=True)

my_instance = MyModel(duration='P7D')
datetime(2014, 1, 1) + my_instance.duration

If I do this, I get an Error:
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'str'

The same happens if I call the obejcts save method:
AttributeError: 'str' object has no attribute 'days' [save_traceback.txt](https://github.com/thecut/thecut-durationfield/files/421942/save_traceback.txt)

see attached save_traceback.txt

Obviously the 'P7D' string is not converted to relativedelta.

When I do this:

my_instance = MyModel(duration=relativedelta.relativedelta(days=7))
datetime(2014, 1, 1) + my_instance.duration
my_instance.save()

The above code works, also the save method, but only until I fetch the
entry from the Database:

In [15]: my_instance = MyModel.objects.all()[0]
In [16]: my_instance.duration
Out[16]: u'P7D'

Here the string is not converted back to a relativedelta object.

Any support would be highly appreciated!

Best regards,
Juergen

@KyeRussell KyeRussell self-assigned this Aug 17, 2016
@mattaustin
Copy link
Contributor

mattaustin commented Aug 26, 2016

Thanks for the report. I can also replicate this using Django 1.8.

It does work correctly in version 1.0.8 (which was never pushed to pypi), and is incorrect in 2.0 - so it's a regression.

I suspect this could be related to the removal of SubfieldBase in commit 29f48ab. The docs at https://docs.djangoproject.com/en/1.8/howto/custom-model-fields/#converting-values-to-python-objects state:

Changed in Django 1.8:

Historically, Django provided a metaclass called SubfieldBase which always called to_python() on assignment. This did not play nicely with custom database transformations, aggregation, or values queries, so it has been replaced with from_db_value().

But nothing was modified on the field to provide from_db_value.

Commit 13a3a34 is also slightly worrying, as it claims 'some fixes' for things which aren't described / referenced / tested.

@g-as
Copy link
Contributor

g-as commented Apr 14, 2017

@mattaustin could you open a PR with your fixes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants