user8878908
user8878908

Reputation:

Django UpdateView without form to update object

I am using class based views with django 1.9 and I am trying to figure out how to update an object(After clicking a button) without using the form. I do not need any user input to update the object. Can you help me?

In models.py

class State(models.Model):
stateID = models.SmallIntegerField(primary_key=True)
isOpen = models.BooleanField(default=True, help_text='Designates whether the registration is open.', verbose_name='active')

def __unicode__(self):
    return bool(self.isOpen)

In views.py

class OpenTournament(View):
    model = State

    def get(self, request, *args, **kwargs):
        queryset = State.objects.all()

        if queryset.count() != 1:
            State(stateID=1, isOpen=True).save()
            return HttpResponseRedirect('/success_url/')

        else:
            #need to update the table from a button click
            return HttpResponseRedirect("/updated/")

Upvotes: 1

Views: 3242

Answers (1)

user8878908
user8878908

Reputation:

Yes. I found it. Access the State by primary key

open = State.objects.get(id=1)
open.isOpen = True
post.save()

Upvotes: 1

Related Questions