0

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
  • I'm not sure what your question is, but your custom save logic will never be called, because you always pass `commit=False`. – Daniel Roseman May 04 '18 at 09:27
  • Hi @Daniel Roseman, isn't this the way it should be done? I'm not sure if I misunderstood how this should be done, but I took the best answer from this post as my reference: https://stackoverflow.com/a/817364/9252255 –  May 04 '18 at 15:21
  • As I said in my comment, your custom logic is all protected by `if commit`. But you pass `commit=False`. So it will never be executed. Why do you use commit=False anyway? – Daniel Roseman May 04 '18 at 15:30
  • Hi Daniel, oh yes now I understand what you meant. I don't know how this got in there, but you are right, it should be without the commit=false in the views.py I think this came when I tried to solve this with pre_save. My question in general was if this approach here is considered as 'good' or if I should rather update this new foreign_key directly in the views.py or in pre_save –  May 04 '18 at 15:57
  • Hi @DanielRoseman, I think this should work or did i misunderstood something? Because after `t = transaction_profile.save(commit=False)` he is calling `t.save()` and the save() method has `commit=True` as a default value – Nepo Znat May 04 '18 at 19:34
  • 1
    No. Look at what your save method returns: `obj`, which is an instance of the model. So `t.save()` calls the model save, not the form one. – Daniel Roseman May 04 '18 at 20:38
  • Ahh now it makes sense, thanks for the explanation – Nepo Znat May 04 '18 at 23:06

0 Answers0