Friday, July 25, 2014

Re: Django request.GET and jQuery Datatables

Thanks Tom, now I see your point.
As my API was crafted on the previous version of Datatables, and used only for that purpose, I prefer just to replace it with the new one.
Had I already an API for whatever reason, I see the usefulness and the importance of your approach. I understand the logic, but I'm still not sure how I could code it.

The QueryDict you point out is in my case `request.GET`.

new_request_get = QueryDict(mutable=True)
new_request_get['new_key'] = request.GET['old_key']
# ...
# Set perhaps mutable=False
form = DataTablesForm(new_request_get)

Is what you mean something like that?

Thanks,
Andrea


2014-07-25 16:28 GMT+02:00 Tom Evans <tevans.uk@googlemail.com>:
On Fri, Jul 25, 2014 at 2:46 PM, Andrea <andrea.ge85@gmail.com> wrote:
> Hi Tom,
>
> thanks for your answer.
> I'm not sure I got it right, do you mean something like the following?
>
> class DataTablesForm(forms.Form):
>     def __init__(self, *args, **kwargs):
>         super(DataTablesForm, self).__init__(*args, **kwargs)
>
>         self.fields['_'] = forms.CharField()
>
>         for i in range(0, 6):
>             self.fields['columns[%d][data]' % i] =
> forms.CharField(required=False)
>             self.fields['columns[%d][name]' % i] =
> forms.CharField(required=False)
>             self.fields['columns[%d][orderable]' % i] =
> forms.BooleanField(required=False)
>             self.fields['columns[%d][search][regex]' % i] =
> forms.BooleanField(required=False)
>             self.fields['columns[%d][search][value]' % i] =
> forms.CharField(required=False)
>             self.fields['columns[%d][searchable]' % i] =
> forms.BooleanField(required=False)
>
>             self.fields['order[%d][column]' % i] =
> forms.IntegerField(required=False, min_value=0, max_value=5)
>             self.fields['order[%d][dir]' % i] =
> forms.CharField(required=False, choices=(('asc', 'asc'), ('desc', 'desc')))
>
>         self.fields['search[regex]' % i] =
> forms.BooleanField(required=False)
>         self.fields['search[value]' % i] =
> forms.BooleanField(required=False)
>
>         draw = forms.IntegerField()
>         length = forms.IntegerField()
>         start = forms.IntegerField()
>
> Cheers,
> Andrea

You could do it that way, but no, that is not what I meant.

You have an external library that is producing data in one format. You
want to use that library, without changing the library itself, in
another library (django) that expects a different format.

The interface for inserting information into a Form is the QueryDict.
Start with the QueryDict that django decodes from the request, and
rewrite the key names so that they now match up with the format that
Django allows for forms (and matching your class).

The alternative way is dynamically producing the fields so that you
can create fields with unpythonic names. Personally, I think it is
cleaner to transform the data to match your API than munge the API to
match your data.

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1LC_V8_ZMcW%2B5GUnQ5r1NfX%2BAs-cg5s_JnHE9vKDhY2_g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAPQ7Y3L2HwhTR%2B%2BxcEM2r7e9m%3D5B-Y4JswtCQ8g7Nw8Stw4Kw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment