Friday, July 25, 2014

Re: Django request.GET and jQuery Datatables

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.

No comments:

Post a Comment