Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Form helper functions Django

5 views
Skip to first unread message

Schalk Neethling

unread,
Jul 7, 2014, 10:25:10 AM7/7/14
to dev-webdev
Hey All,

I have looked at some of the code in our Socorro base and did some
reading online but I cannot seem to find a useful resource for this so I
thought I would ask on the list.

In forms.py there are now two places that uses this exact code:

http://sneethling.pastebin.mozilla.org/5529334

This smells of refactoring into a reusable bit but, is there a specific
way this is handled/done in Django?

Thanks!

--
Kind Regards
Schalk Neethling
Lead Front-End Engineer
Mozilla

Paul McLanahan

unread,
Jul 7, 2014, 10:35:04 AM7/7/14
to dev-webdev
Hi Schalk,

Probably best thing is to put those methods in a mixin class and add
them to the inheritance of your form classes. You could also make a Form
subclass (e.g. DateRangeForm) that adds those methods, then inherit from
that, but I've not gone through the Form metaclass craziness to verify
if that would work without side-effect.

I'm pretty sure either strategy would work though, and it should be easy
to test.

HTH,

pmac

Jannis Leidel

unread,
Jul 7, 2014, 11:06:19 AM7/7/14
to Schalk Neethling, dev-webdev
The best way to make something like this reusable is to put it in a custom form field class. Django documentation is rather sparse about that, sadly: https://docs.djangoproject.com/en/dev/ref/forms/fields/#creating-custom-fields

All you’d have to do is to subclass the DateTimeField class and override the to_python method in the field (make sure to get the original value with a super() call).

Here’s what the current DateTimeField does: https://github.com/django/django/blob/1a2939bc26b7077e3196cea9bce52826ec7a6d9d/django/forms/fields.py#L455-L491

Since you want to check for a value of one field in the clean method of another I could also see this being applied to a subclass of a SplitDateTimeField, a special form field that consists of multiple form inputs (“MultiValueField”). In that case all you’d have to do is to implement the compress method and handle the values passed as a list the way you do in your example.

Here’s the source code of the current validation steps: https://github.com/django/django/blob/1a2939bc26b7077e3196cea9bce52826ec7a6d9d/django/forms/fields.py#L1081-L1114

Cheers,
Jannis


On 07.07.2014, at 16:25, Schalk Neethling <sneet...@mozilla.com> wrote:

> Hey All,
>
> I have looked at some of the code in our Socorro base and did some reading online but I cannot seem to find a useful resource for this so I thought I would ask on the list.
>
> In forms.py there are now two places that uses this exact code:
>
> http://sneethling.pastebin.mozilla.org/5529334
>
> This smells of refactoring into a reusable bit but, is there a specific way this is handled/done in Django?
>
> Thanks!
>
> --
> Kind Regards
> Schalk Neethling
> Lead Front-End Engineer
> Mozilla
> _______________________________________________
> dev-webdev mailing list
> dev-w...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-webdev

0 new messages