I have a method called complete in my model, how can i use that in my class view, in my model method there is one parameter called person which is being passed i do not want my overriden method to use that parameter how can i acheive that.
class Mymodel(models.Model):
is_done = model.BooleanField()
def complete(self, person):
self.is_done = True
self.save(update_fields=['is_done'])
self.done_by.add(person)
class MyView(SomeView):
def complete_record(self):
return Mymodel.complete(here it expects two arguments i need only self)
and i want to get rid of self.done_by.add(person) in model's complete method
CodePudding user response:
You can add a second method to your Model, which is not using the person argument.
models.py
class Mymodel(models.Model):
is_done = model.BooleanField()
def complete(self, person):
self.is_done = True
self.save(update_fields=['is_done'])
self.done_by.add(person)
def complete_without_person(self):
self.is_done = True
self.save(update_fields=['is_done'])
The method has to be called for specific instance of you model in your view.
class MyView(SomeView, model_id):
def complete_record(self):
return Mymodel.objects.get(pk=model_id).complete_without_person()
CodePudding user response:
The complete()
method can be called for a single model instance (a single object of the queryset).
In the View, maybe you want to do this if you have a id param in url:
instance = Mymodel.objects.get(pk=self.kwargs['id'])
instance.complete()
or the scenario can be this if you are using DetailView:
self.get_object().complete()
-- EDIT --
If you want to set is_done = True
without add person and
if you can't edit model class, you can put the logic in the view:
class MyView(SomeView):
def complete_record(self):
record = Mymodel.objects.get(pk=id)
record.is_done = True
record.save()
return record