title: Django model form is_valid() in specific database
body: <p>I have two different database connections defined in my <code>settings.py</code> ('default' and 'banco1').</p>
  
  <p>Then in some view I receive a POST containing some data that I want to validate against a <code>ModelForm</code> that I create:</p>
  
  <pre><code> product_form = ProductForm(request.POST)
   if product_form.is_valid():
  </code></pre>
  
  <p>The product model:</p>
  
  <pre><code>class Product(models.Model):
  
      name = models.CharField(max_length=125, blank=False, null=False)
      description = models.CharField(max_length=255, blank=True)
      abbreviation = models.CharField(max_length=15, blank=True)
      category = models.ForeignKey('inventory.Category',
          on_delete=models.SET_NULL, null=True, blank=True)
      metric = models.CharField(max_length=15, blank=True, null=True)
  </code></pre>
  
  <p>The Form:</p>
  
  <pre><code>class ProductForm(ModelForm):
      class Meta:
          model = Product
          fields = ['name', 'description', 'abbreviation', 'category', 'metric']
  </code></pre>
  
  <p>The view:</p>
  
  <pre><code> def create(request):
          if request.method != 'POST':
              return JsonResponse('Operação inválida', safe=False)
  
          product_form = ProductForm(request.POST)
          if product_form.is_valid():
              f = product_form.save(commit = False)
              f.save(using=request.session['database'])
              return HttpResponse("Novo product salvo com sucesso")
          else:
              return HttpResponse("Impossível salvar product. Verifique os campos")
  </code></pre>
  
  <p>The Product model is suposed to be saved always in the 'banco1' database, so the FK validations should be queried from the 'banco1' database, but whenever I try to validade with <code>product_form.is_valid()</code> Django tries to query the 'default' database and throws a validation error as the FK constraint can't be satisfied in the 'default' database.</p>
  
  <p>How can I set a model form to make his validations agains a manually selected database?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Was suggested to me to utilize a DB ROUTER schema to being able to select the proper database config, which works fine. My problem is that this selection should be made based in some criteria that not just the model. How can I pass some variables from the view to the <code>db_for_read</code> method in my db router class?</p>
  
  <p><strong>EDIT2</strong></p>
  
  <p>New ProductForm Class</p>
  
  <pre><code>class ProductForm(ModelForm):
      class Meta:
          model = Product
          fields = ['name', 'description', 'abbreviation', 'category', 'metric']
  
      def __init__(self,database):
          category = forms.ModelChoiceField(queryset=Category.objects.using(database).all())
  </code></pre>
  
  <p>trying to instantiate the form inside my view like this:</p>
  
  <pre><code>product_form = ProductForm(request.POST, request.session['database'])
  </code></pre>
  
  <p>Which gives me an error:</p>
  
  <blockquote>
    <p><strong>init</strong>() takes 2 positional arguments but 3 were given</p>
  </blockquote>
  
  <p>I know that something is not valid with my manner to override the <code>__init__</code> method but i'm to noobish in python to realize that.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>Last errors:</p>
  
  <blockquote>
    <p>Traceback:</p>
    
    <p>File "C:\Users\Pavarine\Documents\Visual Studio
    2015\Projects\gims\gims\env\lib\site-packages\django\core\handlers\base.py"
    in get_response
      149.                     response = self.process_exception_by_middleware(e, request)</p>
    
    <p>File "C:\Users\Pavarine\Documents\Visual Studio
    2015\Projects\gims\gims\env\lib\site-packages\django\core\handlers\base.py"
    in get_response
      147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)</p>
    
    <p>File "C:\Users\Pavarine\Documents\Visual Studio
    2015\Projects\gims\gims\gims\apps\inventory\views.py" in create
      247.         if product_form.is_valid():</p>
    
    <p>File "C:\Users\Pavarine\Documents\Visual Studio
    2015\Projects\gims\gims\env\lib\site-packages\django\forms\forms.py"
    in is_valid
      161.         return self.is_bound and not self.errors</p>
    
    <p>File "C:\Users\Pavarine\Documents\Visual Studio
    2015\Projects\gims\gims\env\lib\site-packages\django\forms\forms.py"
    in errors
      153.             self.full_clean()</p>
    
    <p>File "C:\Users\Pavarine\Documents\Visual Studio
    2015\Projects\gims\gims\env\lib\site-packages\django\forms\forms.py"
    in full_clean
      362.         self._clean_fields()</p>
    
    <p>File "C:\Users\Pavarine\Documents\Visual Studio
    2015\Projects\gims\gims\env\lib\site-packages\django\forms\forms.py"
    in _clean_fields
      374.                 value = field.widget.value_from_datadict(self.data, self.files,
    self.add_prefix(name))</p>
    
    <p>File "C:\Users\Pavarine\Documents\Visual Studio
    2015\Projects\gims\gims\env\lib\site-packages\django\forms\widgets.py"
    in value_from_datadict
      231.         return data.get(name)</p>
    
    <p>Exception Type: AttributeError at /inventory/product/create Exception
    Value: 'tuple' object has no attribute 'get'</p>
  </blockquote>