We have until recent used Django REST API for creating a dynamic form,
using the in built HTML Form generation for a model Machine as part of an
selfservice site:
models.py:
class Middleware(models.Model):
name = models.CharField(unique=True, max_length=10, verbose_name=
'Middleware')
class TshirtSize(models.Model):
size = models.CharField(unique=True, max_length=10, verbose_name='Size')
class Stage(models.Model):
stage = models.CharField(unique=True, max_length=1, verbose_name='Stage'
)
class Machine(models.Model):
name = models.CharField(unique=True, max_length=10, verbose_name='Name')
middleware = models.ForeignKey(Middleware, to_field='name', blank=True,
null=True, verbose_name='Middleware')
tshirt_size = models.ForeignKey(TshirtSize, to_field='size', blank=True,
null=True, verbose_name='T-Shirt-Size')
stage = models.ForeignKey(Stage, to_field='stage', verbose_name='Stage')
class MiddlewareSpecification():
middleware = models.ForeignKey(Middleware, to_field='name', verbose_name
='Middleware')
tshirt_size = models.ForeignKey(TshirtSize, to_field='size',
verbose_name='T-Shirt-Size')
stage = models.ForeignKey(Stage, to_field='stage', verbose_name='Stage')
Serializers are pure ModelSerializers as described here [1].
For the frontend part we used jquery code to replace existing form on the
fly.
function replaceForm(data) {
formContainer.html(data);
form = new Form($('#instanceForm'));
}
Dynamic form means that when user selects a "tshirt_size",
formfield.onSelect triggers further filtering with that query for formfield
"stage".
Filter is done using a second model "MiddlewareSpecification" for
crosstable relation of the attributes that gets filtered.
Now for flexibility reasons we want to switch using JSON Serializer and
OPTIONS HTTP Verb, to retrieve information about the form layout.
Middleware Spec contains data like that:
First idea is to put further logic in the Frontend to retrieve multiple
choice field specific values in seperate calls from different viewsets.
That means having a viewset for each selectable formfield middleware,
stage, tshirt_size.
Benefit is that we don't reload the whole form data as we only need to get
the filtered data of the selectable fields.
But that comes to the price of much more frontend logic.
What other possibilities do we have?
Is it possible to use MiddlewareSpec as nested Serializer in
MachineSerializer?
Is it possible with the default REST scheme to overwrite the "formular
layout" generated by HTTP Verb "Options" to include the selectable choices?
Would HypermediaModelSerializers be useful to reduce the frontend code from
harcoded links?
[1]
http://www.django-rest-framework.org/api-guide/serializers/#modelserializer
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.