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

Add choice fields #15

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

lieryan
Copy link

@lieryan lieryan commented Sep 29, 2020

I know there has been a couple attempts on making a form field for this library, including a very recent one by GigiusB, and a couple older ones by lancondrej and ChandlerBent.

This PR implements a slightly different requirement than those, in that this adds a RelativeDeltaChoiceField form field that uses <select> widget to pick from a list of predetermined durations instead of a free field arbitrary durations.

Lie Ryan added 9 commits September 29, 2020 05:03
This is more backwards compatible up to Django 2.0 and 2.1.
register_serializer() is available on Django 2.2 and up, and generally
produces a better/more readable migrations than deconstructible(), which
had been available longer. The migration should be compatible either
way, so let's prefer a more readable migration when it's available.
Copy link
Contributor

@sjamaan sjamaan left a comment

Choose a reason for hiding this comment

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

Note that I am still waiting for @GigiusB to submit a new PR for his version of django-admin integration, so I'll have to run this one by him. He's currently putting a lot of effort in and it would be a shame for that to go to waste.

Nevertheless, what I'm confused about is what happens in the admin UI when there aren't any choices in a relativedeltafield. Where's the code that makes this happen? It just handles it by default?

Anyway, this doesn't seem to work as intended. When you click on "add" for interval (without choices), it shows you a form and you must type in an ISO interval. But then when you type in, say P5M and click "save and continue editing", it shows you relativedelta(months=+5), which cannot be saved. I think this has something to do with the RelativeDeltaSerializer.


elif has_migration_deconstructible:
# Django 2.1 and lower
deconstructible(relativedelta)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this migration stuff needed, exactly?

I've used django-relativedelta in a few projects and it never required any special code.

Copy link
Author

Choose a reason for hiding this comment

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

makemigrations does not know how to serialise relativedelta if the model fields' arguments contains a relativedelta object, and this tells it how. choices are defined by a "list of (relativedelta, str) pair", so we need this when choices is defined. Without this, you'd get an error when doing makemigrations:

ValueError: Cannot serialize: relativedelta(days=+1)
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/3.1/topics/migrations/#migration-serializing

Without this serialisation stuffs, default wouldn't work either RelativeDeltaField(default=relativedelta(days=1)).

@lieryan
Copy link
Author

lieryan commented Sep 30, 2020

Thank you for reviewing my PR.

when there aren't any choices in a relativedeltafield. Where's the code that makes this happen? It just handles it by default?

When the model field isn't restricted by choices, editing still won't work as previously. I expect that GigiusB will be implementing that part, so I didn't do anything there. Functionality-wise, this PR doesn't actually overlap much with @GigiusB's or the other PRs (except for the general theme of editing). GigiusB's PR implements editing of unrestricted duration field (or when restricted only by validators), my PR doesn't do anything for editing unrestricted duration field.

My PR only implements admin editing and ModelForm generation when the field has choices, in this situation Django uses a <select> field rather than <input> field:

image

Given that forms.CharField and forms.ChoiceField works very differently anyway and the bulk of RelativeDeltaChoiceField is just calling pre-existing functions (parse_relativedelta/format_relativedelta), I don't expect there would be much in common between my PR and GigiusB's.

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