0

In my application there must be two inputs which will get numbers, and when I'll clickcreate button in the given range (which was written in inputs) difference of that number times object must be created. For example, in first input I'll write "2", in second "5", 3 objects must be created. Here are my codes:

models.py:

class Blank(models.Model):
    blank_series = models.CharField(_('Blankın seriyası'), max_length=3)
    number_from = models.IntegerField()
    number_to = models.IntegerField()
    
    def __str__(self):
        return self.blank_series

forms.py:

from django.forms import ModelForm
from .models import Blank

class CreateBlankForm(ModelForm):
    class Meta:
        model = Blank
        fields = '__all__'

html:

<tr>
  <td>{{ form.blank_series.label }}</td>
  <td>{{ form.blank_series }}</td>
</tr>
<tr>
  <td>{{ form.blank_number.label }}</td>
  <td>
    {{ form.number_from }}
    {{ form.number_to }}
  </td>
</tr>
<tr>
  <td class="create-blank-btns">
    <button class="create-blank-btn"><span class="material-icons">add_circle_outline</span><input type="submit" value="Əlavə et" name="create-blank"></button>
  </td>
</tr>

views.py:

@login_required(login_url='sign-in')
def blanks(request):

    if request.method == 'POST':
        form = CreateBlankForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = CreateBlankForm()

    blanks = Blank.objects.all()
    
    context = {
        'form': form,
        'blanks': blanks,
    }
    return render(request, 'blank.html', context)
5
  • what object you want to create multiple times? Commented Jul 23, 2020 at 11:55
  • You want to create Blank object multiple times ? Commented Jul 23, 2020 at 12:36
  • @arjun, yes, but in each loop blank's series must be changed. Number between begininng and end of range must be assigned to series Commented Jul 23, 2020 at 12:39
  • What is blank series?what will be changed in blank series ? Commented Jul 23, 2020 at 13:03
  • @arjun, OH MY GOD, man ! look at the codes please, if you want to help. Commented Jul 23, 2020 at 13:09

1 Answer 1

1

you can do this in two ways 1)override save method in model form 2)get cleaned data in the view and save multiple times

1)override save method in model form in the model form

class CreateBlankForm(ModelForm):
    class Meta:
        model = Blank
        fields = '__all__'
    def save(self, *args, **kwargs):
        times = self.number_to - self.number_from
        for i in times:   
            super(CreateBlankForm).save(*args, **kwargs)
    

2)get the cleaned data on the view get the cleaned data and loop over in save method

@login_required(login_url='sign-in')
def blanks(request):
    if request.method == 'POST':
        form = CreateBlankForm(request.POST)
        if form.is_valid():
            times = form.cleanded_data['number_to'] - form.cleaned_data['number_from']
            for i in times:
                form.save()
    else:
        form = CreateBlankForm()
    blanks = Blank.objects.all()
    context = {
        'form': form,
        'blanks': blanks,
    }
    return render(request, 'blank.html', context)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.