How to use two different models in CreateView in django
<p>I have following models:</p>

<pre><code>class BankAccount(models.Model):
    owner = models.ForeignKey(User)

class MoneyTransfer(models.Model):
    sender = models.ForeignKey(BankAccount)
</code></pre>

<p>and url:</p>

<pre><code>url(r'^accounts/(?P&lt;pk&gt;\w+)/send_transfer$', SendTransfer.as_view(), name='SendTransfer')
</code></pre>

<p>that means "I want to send money from Account with id=pk"</p>

<p>This is my view:</p>

<pre><code>class SendTransfer(View):
    form_class = SendTransferForm
    template_name = 'dashboard/send_transfer.html'

    def get(self, request, *args, **kwargs):
        instance = BankAccount.objects.get(id=self.kwargs['pk'])
        if instance.is_legal():
            if instance.organization.owners.all().filter(user__id=self.request.user.id).count() == 0:
                return None
        else:
            if instance.citizen.user.id != self.request.user.id:
                return None
        return render(self.request, self.template_name, self.get_context_data())

    def post(self, request, *args, **kwargs):
        sender = BankAccount.objects.get(id=kwargs['pk'])

        form = self.form_class(sender, self.request.user, request.POST)
        if form.is_valid():
            MoneyTransfer.objects.create(sender=sender,
                                     receiver=BankAccount.objects.get(id=self.request.POST['receiver']),
                                     total=float(self.request.POST['total']),
                                     when=timezone.localtime(timezone.now()),
                                     comment=self.request.POST['comment'])
            return redirect('AccountDetail', kwargs['pk'])

        data = self.get_context_data()
        data['form'] = form
        return render(request, self.template_name, data)

    def get_context_data(self):
        account = BankAccount.objects.get(id=self.kwargs['pk'])
        return {'form': SendTransferForm(account, self.request.user),
            'user': self.request.user,
            'account': account}
</code></pre>

<p>I think there's a lot of redudant code for CBV. What can I do for shorting it?</p>

<p><strong>UPD</strong></p>

<p>my current code:</p>

<pre><code>class SendTransfer(SingleObjectMixin, FormView):
model = BankAccount
form_class = SendTransferForm
template_name = 'dashboard/send_transfer.html'

def dispatch(self, request, *args, **kwargs):
    self.object = self.get_object()
    return super(SendTransfer, self).dispatch(request, *args, **kwargs)

def get_object(self, queryset=None):
    obj = super(SendTransfer, self).get_object(queryset)

    if obj.is_legal():
        if not obj.organization.owners.filter(user=self.request.user).exists():
            raise Http404
    else:
        if obj.citizen.user != self.request.user:
             raise Http404

    return obj

def form_valid(self, form):
    data = form.cleaned_data
    MoneyTransfer.objects.create(sender=self.object,
                                 receiver=data['receiver'], # ModelChoiceField in the form
                                 total=data['total'], # FloatField in the form, etc.
                                 when=timezone.localtime(timezone.now()),
                                 comment=data['comment'])
    return redirect('AccountDetail', self.object.pk)
</code></pre>

<p>last line of dispatch() method raises TypeError: <strong>init</strong>() takes at least 3 arguments (1 given)</p>