my project has two steps 1) database entrance for order model 2) database entrance for transaction profile. Same time 1) will be updated with the foreign key to 2)
I tried to keep my view.py as clean as possible and modified the save() function. However, I have read that this is not always recommended and I wondered if you have any feedback to my approach below or if you would have done it any different.
views.py
def checkout_page(request):
session_order_id = request.session['order_id']
if request.POST:
transaction_profile = TransactionProfileModelForm(request, request.POST)
if transaction_profile.is_valid():
t = transaction_profile.save(commit=False)
t.save()
else:
transaction_profile = TransactionProfileModelForm(request)
forms.py
from orders.models import Order
class TransactionProfileModelForm(forms.ModelForm):
email_confirm = forms.EmailField()
class Meta:
model=TransactionProfile
fields = [
'email',
'email_confirm',
'address_line_1',
'address_line_2',
'city',
'country',
'postal_code',
'state'
]
def __init__(self, request, *args, **kwargs):
self.request = request
super(TransactionProfileModelForm, self).__init__(*args, **kwargs)
def save(self, commit=True):
obj = super(TransactionProfileModelForm, self).save(commit=False)
if commit:
obj.save()
request = self.request
session_order_id = request.session['order_id']
o = Order.objects.get(order_id=session_order_id)
o.transaction_profile = obj
o.save()
return obj