I would like to solve a problem in form handling my views.py.
I would like to process a ModelForm. Instead of letting the user fill the field Field fk_transaction I would like to create a new Record of Transaction and add it as a ForeignKey in the background to the CcountBooking. The Form is displayed correctly in the the template. Also a Transaction is successfully created. However when I want to add and save it to the form in views.py it throws an error telling me that the field is not existing. Thanks for any help!
My views.py:
from django.http import HttpResponse
from django.shortcuts import render
from forms import AccountBookingForm
from models import Transaction
def deposit(request):
context = {}
if request.method == "POST":
form = AccountBookingForm(request.POST)
t = Transaction()
form.fk_transaction = t
form.save()
return render(request, "deposit_success.html", {"form": form})
else:
form = AccountBookingForm()
return render(request, "deposit.html", {"form": form})
My models.py:
class Transaction(models.Model):
type = models.CharField(max_length=11, choices=TR_TYPE_CHOICES, default=DEPOSIT)
status = models.CharField(max_length=11, choices=TR_STATUS_CHOICES, default=DRAFT)
text = models.TextField(max_length=500, default="", blank=True)
class AccountBooking(models.Model):
fk_bankaccount = models.ForeignKey(
BankAccount, on_delete=models.CASCADE, blank=False)
fk_transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, blank=False)
value = models.DecimalField(max_digits=14, decimal_places=2, blank=False)
My forms.py
from django.forms import ModelForm
from .models import AccountBooking
class AccountBookingForm(ModelForm):
class Meta:
model = AccountBooking
fields = ["__all__"]
exclude = ("fk_transaction",)
Check the form, and create a transaction if the form is valid:
from django.shortcuts import redirect, render
from forms import AccountBookingForm
from models import Transaction
def deposit(request):
if request.method == 'POST':
form = AccountBookingForm(request.POST, request.FILES)
if form.is_valid():
form.instance.fk_transaction = Transaction.objects.create()
form.save()
return redirect('name-of-view')
else:
form = AccountBookingForm()
return render(request, 'deposit.html', {'form': form})
Note: While most forms do not process media files, it is probably better to pass
request.FILES
[Django-doc] to the form anyway, such that if you later add an extra media field, all views that use the form will indeed handle the files properly.
Note: In case of a successful POST request, you should make a
redirect
[Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.