<div class="col-3 text-center">
<h5>Child #1: </h4>
</div>
<div class="col-2 text-center">
{{ child.kg }}
</div>
<div class="col-2 text-center" >
{{ child.material }}
</div>
<div class="col-2 text-center" >
{{ child.box_price }}
</div>
<div class="col-3 text-center">
<button type="button" id='remove_box_0' class="btn btn-danger">Remove box</button>
</div>
{% endfor %}
</div>
</div>
</div>
<p>
{{ child_form.management_form }}
<input id="create" class="btn btn-secondary btn-lg btn-block btn-option btn-form" type="submit" value="Crear" />
</p>
javascript
// Adds the difference between desired boxes and current boxes
function add_boxes(form_id, total, num){
for (let i = total; i != total+num; i++) {
var element = $('#nested_forms_' + form_id + "> :first-child ").clone().css('display', 'none');
element.appendTo('#nested_forms_' + form_id).show('250');
};
$('#id_child-TOTAL_FORMS').val(num + total);
};
// Removes every unneeded boxes
function remove_boxes(form_id, total){
$('#nested_forms_' + form_id).children().each(function(){
if ($(this).index() >= total) {
$(this).fadeTo(150, 0).slideUp(150, function(){
$(this).remove();
});
}
});
$('#id_child-TOTAL_FORMS').val(total);
}
/r/django
https://redd.it/8ggdy2
<h5>Child #1: </h4>
</div>
<div class="col-2 text-center">
{{ child.kg }}
</div>
<div class="col-2 text-center" >
{{ child.material }}
</div>
<div class="col-2 text-center" >
{{ child.box_price }}
</div>
<div class="col-3 text-center">
<button type="button" id='remove_box_0' class="btn btn-danger">Remove box</button>
</div>
{% endfor %}
</div>
</div>
</div>
<p>
{{ child_form.management_form }}
<input id="create" class="btn btn-secondary btn-lg btn-block btn-option btn-form" type="submit" value="Crear" />
</p>
javascript
// Adds the difference between desired boxes and current boxes
function add_boxes(form_id, total, num){
for (let i = total; i != total+num; i++) {
var element = $('#nested_forms_' + form_id + "> :first-child ").clone().css('display', 'none');
element.appendTo('#nested_forms_' + form_id).show('250');
};
$('#id_child-TOTAL_FORMS').val(num + total);
};
// Removes every unneeded boxes
function remove_boxes(form_id, total){
$('#nested_forms_' + form_id).children().each(function(){
if ($(this).index() >= total) {
$(this).fadeTo(150, 0).slideUp(150, function(){
$(this).remove();
});
}
});
$('#id_child-TOTAL_FORMS').val(total);
}
/r/django
https://redd.it/8ggdy2
reddit
r/django - Not getting the context data of dynamically-generated formset
0 votes and 0 so far on reddit
Question regarding nested serializers
I'm building an API for a web application that I'm working on where I'm using a nested relationship for a create end point like the one here in the DRF framework
​
[https://www.django-rest-framework.org/api-guide/relations/#nested-relationships](https://www.django-rest-framework.org/api-guide/relations/#nested-relationships)
​
I want to use the serializer for updating, so I'm using a code like this:
def update(self, instance, validated_data):
tracks_data = validated_data.pop('tracks')
tracks = instance.tracks.all()
tracks = list(tracks)
instance.album_name = validated_data.get('album_name', instance.album_name)
instance.artist = validated_data.get('artist', instance.artist)
instance.save()
for track_data in tracks_data:
track = tracks.pop(0)
track.order = track_data.get('order', track.order)
track.title = track_data.get('title', track.title)
track.duration = track_data.get('duration', track.duration)
track.save()
/r/django
https://redd.it/izozan
I'm building an API for a web application that I'm working on where I'm using a nested relationship for a create end point like the one here in the DRF framework
​
[https://www.django-rest-framework.org/api-guide/relations/#nested-relationships](https://www.django-rest-framework.org/api-guide/relations/#nested-relationships)
​
I want to use the serializer for updating, so I'm using a code like this:
def update(self, instance, validated_data):
tracks_data = validated_data.pop('tracks')
tracks = instance.tracks.all()
tracks = list(tracks)
instance.album_name = validated_data.get('album_name', instance.album_name)
instance.artist = validated_data.get('artist', instance.artist)
instance.save()
for track_data in tracks_data:
track = tracks.pop(0)
track.order = track_data.get('order', track.order)
track.title = track_data.get('title', track.title)
track.duration = track_data.get('duration', track.duration)
track.save()
/r/django
https://redd.it/izozan
www.django-rest-framework.org
Serializer relations - Django REST framework
Django, API, REST, Serializer relations