Django UpdateView create new object
<p>My problem:
UpdateView create new object instead of updating previous, i think its happens because in class definition of my view i override get_object method like this:</p>

<pre><code>def get_object(self, queryset=None):
    try:
        object_get = self.model.objects.get(pk=self.kwargs['pk'])
    except ObjectDoesNotExist:
        raise Http404("No object found matching this query")

    if self.request.user.is_authenticated():
        if object_get.owner == self.request.user:
            return object_get
</code></pre>

<p>And so if current user is not owner of the object - this method return nothing - its what i wanted, but my form class instead create new object: </p>

<pre><code>class ClientCreation(forms.ModelForm):

    class Meta:
        model = Client
        fields = ('name', 'loyal')
</code></pre>

<p>I think this is happened because form doesn't receive a self.instance and create new instead - what should i do in this situation? I don't want new object to be created, in case when owner of object is not the current user i want nothing to happend then sending such a post request. How should i correctly implement this?</p>

<p>UPDATE views.py:</p>

<pre><code>class Distinct(generic.UpdateView):
    def get_object(self, queryset=None):
        try:
            object_get = self.model.objects.get(pk=self.kwargs['pk'])
        except ObjectDoesNotExist:
            raise Http404("No object found matching this query")

        if self.request.user.is_authenticated():
            if object_get.owner == self.request.user:
                return object_get

    def get_form_kwargs(self):
        kwargs = super(Distinct, self).get_form_kwargs()
        if self.request.user.is_authenticated():
            kwargs.update({'user': self.request.user})
        return kwargs

    def post(self, request, *args, **kwargs):
        if request.POST.get('action', '') == 'Delete':
            object_get = self.get_object()
            request.session['deleted_data'] = str(object_get)
            object_get.delete()
            return redirect(reverse('crm:main'))
        else:
            return super(Distinct, self).post(request, *args, **kwargs)

    def get_success_url(self):
        return reverse('crm:{}'.format(self.distinct_template), kwargs={'pk': self.kwargs['pk']})

class DistinctClient(Distinct):

    form_class = ClientCreation
    model = Client
    template_name = 'crm/client_detail.html'
    all_template = 'clients'
    distinct_template = 'client'

    def get_form_kwargs(self):

        return generic.UpdateView.get_form_kwargs(self)
</code></pre>

<p>In <code>UpdateView</code>, if <code>get_object</code> returns <code>None</code> django will create a new object.So instead of return <code>None</code> do whatever you want.</p>

<pre><code>def get_object(self, queryset=None):
    try:
        object_get = self.model.objects.get(pk=self.kwargs['pk'])
    except ObjectDoesNotExist:
        raise Http404("No object found matching this query")

    if self.request.user.is_authenticated():
        if object_get.owner == self.request.user:
            return object_get
    raise My #do something here.
</code></pre>

<p><strong>UPDATE</strong></p>

<pre><code>class My(Exception):
    pass
class DistinctClient(Distinct):

    form_class = ClientCreation
    model = Client
    template_name = 'crm/client_detail.html'
    all_template = 'clients'
    distinct_template = 'client'

    def dispatch(self, *args, **kwargs):
        try:
            return super(DistinctClient, self).dispatch(*args, **kwargs)
        except My:
            return redirect #to do or (return render(self.request, 'mytemplate.html', {}))
</code></pre>

python django django-class-based-views