I've created a maintenance App that allows the user to create and view the maintenance details. I have a page "maintenance-details.html" where I show all the details of a particular maintenance.
Here is my views.py:
class MaintenanceDetailView(DetailView):
template_name = 'maintenance/maintenance-details.html'
model = Maintenance
def get_context_data(self, **kwargs):
contacts_suppliers = ContactsSupplier.objects.filter(supplier__maintenance=self.object)
hora_atual = datetime.datetime.now()
context = super().get_context_data(**kwargs)
context['contacts_supplier'] = contacts_suppliers
context['hora_atual'] = hora_atual
return context
I have created a button on my template named "Mark as done". My Maintenance model has a BooleandField "done" with the purpose to set the task as done or not. What I'm looking for is the best way to update the model and set the "done" as "True" when the user clicks it.
My models.py here:
class Maintenance(models.Model):
category = models.ForeignKey(SuppliersCategory, models.DO_NOTHING, db_column='Category') # Field name made lowercase.
property = models.ForeignKey(Property, models.DO_NOTHING, db_column='Property_Reference') # Field name made lowercase.
name = models.CharField(db_column='Name', max_length=25) # Field name made lowercase.
created_date = models.DateTimeField(db_column='Date', auto_now_add=True) # Field name made lowercase.
staffmember = models.CharField(db_column='StaffMember', max_length=25, blank=True, null=True) # Field name made lowercase.
supplier = models.ForeignKey(Suppliers, db_column='Supplier') # Field name made lowercase.
description = models.CharField(db_column='Description', max_length=500, blank=True, null=True) # Field name made lowercase.
photo = models.ImageField(upload_to='maintenace/', db_column='Photo', blank=True, null=True) # Field name made lowercase.
expirydate = models.DateTimeField(db_column='ExpiryDate', blank=False) # Field name made lowercase.
datecompletion = models.DateTimeField(db_column='DateCompletion', blank=True, null=True) # Field name made lowercase.
done = models.BooleanField(db_column='Done', default=False) # Field name made lowercase.
class Meta:
db_table = 'Maintenance'
def get_absolute_url(self):
return reverse("maintenance:maintenance_detail",kwargs={'pk':self.pk})
def set_done(self):
self.done = True
self.datecompletion = timezone.now()
self.save()
What is the right way to do this?