python - django model instance edit -
i have 3 models set follows:
#models.py class room(models.model): room_number = models.charfield(max_length=20, primary_key=true, null=false, blank=false) isolation_status = models.booleanfield() class treatment(models.model): room_number = models.foreignkey(room, primary_key=true) treated = models.booleanfield() class isolation(models.model): room_number = models.foreignkey(room, primary_key=true)
what need when create new treatment object set room.isolation_status field false.
#views.py class addtxview(formview): template_name = 'addtx.html' form_class = txform success_url = '/rooms/addtx/' def update_iso(self, form): instance = room.objects.get(room_number=room_number) instance.isolation_status = false def form_valid(self, form): save_it = form.save(commit=false) save_it.save() return super(addtxview, self).form_valid(form) #forms.py class txform(forms.modelform): class meta: model = treatment
when should call update_iso function? after save()? should using class-based view
you set in model. whenever save treatment, check if new. if is, set isolation_status of room_number false.
#models.py class room(models.model): room_number = models.charfield(max_length=20, primary_key=true, null=false, blank=false) isolation_status = models.booleanfield() class treatment(models.model): room_number = models.foreignkey(room, primary_key=true) treated = models.booleanfield() def save(self, *args, **kwargs): is_new = false if self.pk none: is_new = true super(treatment,self).save(*args, **kwargs) if is_new: self.room_number.isolation_status = false self.room_number.save() class isolation(models.model): room_number = models.foreignkey(room, primary_key=true)
by doing work whenever add treatment (be in views or in admin).
Comments
Post a Comment