title: Form(prefix=) and Race Conditions
body: <p>As a small learning project, I am using <code>Form</code> prefix to submit two separate forms (same Model) as a one-page form. The model <code>Account</code> has a field <code>account_number</code> that increments after each model save. The initial value either 1 or user-defined.</p>
  <p>My issue is that my <code>form2</code> doesn't receive the correct value. I have been trying to use <code>F() expressions</code> and <code>request_from_db()</code> but I'm not sure if I am implementing them properly.</p>
  <p>I have tried using a call back on the default value in the account_number field (below) but I can see that the default account_number on Form2 is the same as Form1. It does change after saving, but If I open two browsers tabs of the form page before I submit the first page, the account_numbers aren't unique.</p>
  <p>My Account model.py</p>
  <pre><code>def account_number_increment():
      last_number = Account.objects.all().order_by(&quot;account_number&quot;).last()
      if last_number:
          print(&quot;before db refresh&quot;, last_number.account_number)
          last_number.refresh_from_db()
          print(&quot;after db&quot;, last_number.account_number)
      start_from = AccountNumber.start_from
      acc_number = '1'
      if start_from:
          while Account.objects.all().filter(account_number=start_from).exists():
              acc_number_start = int(last_number.account_number) + 1
              print(&quot;acc # from start from&quot;, acc_number_start)
              return str(acc_number_start)
      if last_number:
          while Account.objects.all().filter(account_number=last_number.account_number).exists():
              new_acc_number = int(last_number.account_number) + 1
              print(&quot;acc # from last number&quot;, new_acc_number)
              return str(new_acc_number)
      if not last_number or not start_from:
          return acc_number
  
  
  class Account(models.Model):
      uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
      account_number = models.CharField(max_length=20, default=account_number_increment, blank=False, unique=True)
      name = models.CharField(max_length=100, null=True, blank=True)
  
      def __str__(self):
              return self.name
  </code></pre>
  <p>My forms.py</p>
  <pre><code>class AccountForm(ModelForm):
      class Meta:
          model = Account
          fields = ['name', 'account_number']
  </code></pre>
  <p>my views.py</p>
  <pre><code>def index(request):
      if request.method == 'POST':
          form1 = AccountForm(prefix='form1', data=request.POST)
          form2 = AccountForm(prefix='form2', data=request.POST)
          if form1.is_valid() and form2.is_valid():
              form1.save()
              form2.save()            
              return redirect(reverse('users:users_home'))
      else:
          form1 = AccountForm(prefix='form1')
          form2 = AccountForm(prefix='form2')
  
      context = {
          'form1': form1,
          'form2': form2,
      }
      return render(request, 'accounts/index.html', context)
  </code></pre>
  <p>If I override the Model <code>save()</code> only form1 is saved. Not sure what happens to the other one, don't get any errors.</p>
  <pre><code>def save(self, *args, **kwargs):
          last_number = Account.objects.all().order_by(&quot;account_number&quot;).last()
          if last_number:
             if self.account_number == last_number.account_number:
                 print(&quot;FROM MODEL: before db refresh&quot;, last_number.account_number)
                 last_number.refresh_from_db()
                 #self.account_number = last_number.update(account_number=F('account_number') + 1)
                 self.account_number = F('account_number') + 1
                 self.save()
                 print(&quot;FROM MODEL, after db refresh&quot;, last_number.account_number)
                 super(Account, self).save(*args, kwargs)
          else:
              super(Account, self).save(*args, kwargs)
  </code></pre>
  <p><code>Form(commit=False)</code> isn't working for me either. If I open two browsers tabs of the form page before I submit the first page, the account_numbers aren't unique.</p>
  <pre><code>def index(request):
      if request.method == 'POST':
          form1 = AccountForm(prefix='form1', data=request.POST)
          form2 = AccountForm(prefix='form2', data=request.POST)
          if form1.is_valid() and form2.is_valid():
              form1.save()            
              form = form2.save(commit=False)
              last_number = Account.objects.all().order_by(&quot;account_number&quot;).last()
              if last_number:
                  print(&quot;FROM VIEW: before db refresh&quot;, last_number.account_number)
                  last_number.refresh_from_db()
                  print(&quot;FROM VIEW, after db refresh&quot;, last_number.account_number)
              # acc_number_form1 = form1.cleaned_data['account_number']
              
              form.account_number = int(last_number.account_number) + 1
              form.save()
              return redirect(reverse('users:users_home'))
      else:
          form1 = AccountForm(prefix='form1')
          form2 = AccountForm(prefix='form2')
  
      context = {
          'form1': form1,
          'form2': form2,
      }
      return render(request, 'accounts/index.html', context)
  </code></pre>
  <p>I am assuming it needs to be done at the database level with F() and request_from_db()? Any suggestions would be greatly appreciated.</p>