How to update if user exist instead of create?

26 views
Skip to first unread message

Django Lover

unread,
Sep 18, 2018, 8:13:20 AM9/18/18
to Django users
How i can update model when Key exist else create a new one??
 

My code is-
 
**Form.py**

class UserSettingForm(forms.ModelForm):
     class Meta:
           model = StUserSetting
           fields = ( 'default_language', 'prim_currency', 'number_format', 'decimal_format',
            'date_format', 'time_format', 'currency_format', 'currency_position', 'financial_year_start',
             'week_start', 'estimate_default_tab', 'invoice_default_tab',)

**View.py**


class CreateUpadateUserSetting(LoginRequiredMixin, FormView):
     template_name = 'settings/user_settings.html'
     form_class = UserSettingForm
     success_url = '/user/setting/preference/'


Larry Martell

unread,
Sep 18, 2018, 8:27:28 AM9/18/18
to django...@googlegroups.com

Django Lover

unread,
Sep 18, 2018, 8:35:39 AM9/18/18
to Django users
Thanks, Larry for replying. I want to say I am new in Django and I don't know how to use in class-based view, Can you help me to integrate with CBV?

Mateusz

unread,
Sep 18, 2018, 8:09:13 PM9/18/18
to Django users
Just for the record, you should probably consider using CreateView and UpdateView (docs) if possible. ;)
Here is an example (Stack).
Reply all
Reply to author
Forward
0 new messages