Add stock item fields for each item passed to the form

This commit is contained in:
Oliver Walters 2019-05-29 22:38:42 +10:00
parent 0e3f74ef31
commit d321947026
2 changed files with 19 additions and 6 deletions

View File

@ -61,10 +61,15 @@ class MoveStockItemForm(forms.ModelForm):
confirm = forms.BooleanField(required=False, initial=False, label='Confirm Stock Movement', help_text='Confirm movement of stock items')
def __init__(self, *args, **kwargs):
stock_items = kwargs.pop('stock_items', [])
super(MoveStockItemForm, self).__init__(*args, **kwargs)
self.fields['location'].choices = self.get_location_choices()
for item in stock_items:
self.fields['stock_item_{id}'.format(id=item.id)] = forms.IntegerField(required=True, initial=item.quantity, help_text='Quantity for ' + str(item))
class Meta:
model = StockItem

View File

@ -131,6 +131,7 @@ class StockItemMoveMultiple(AjaxView, FormMixin):
ajax_template_name = 'stock/stock_move.html'
ajax_form_title = 'Move Stock'
form_class = MoveStockItemForm
items = []
def get_items(self, item_list):
""" Return list of stock items. """
@ -145,20 +146,27 @@ class StockItemMoveMultiple(AjaxView, FormMixin):
return items
def get_form_kwargs(self):
args = super().get_form_kwargs()
args['stock_items'] = self.get_items(self.items)
return args
def get(self, request, *args, **kwargs):
item_list = request.GET.getlist('stock[]')
# Save list of items!
self.items = request.GET.getlist('stock[]')
items = self.get_items(item_list)
print(items)
return self.renderJsonResponse(request, self.form_class())
return self.renderJsonResponse(request, self.get_form())
def post(self, request, *args, **kwargs):
form = self.get_form()
print(request.POST)
valid = form.is_valid()
print("Valid:", valid)