Error - DETAIL: Failing row contains (6, 2022-06-18 09:50:32.722841 00, 2022-06-18 00:00:00 00, NE, something, something, null). I got 3 models: Patient => Hospitalization&Consults(ForeignKey=Patient), OneToMany. I know there, on "null" spot, must be the Patient ID, but I don t know how to get this ID.
Models.py:
class Patient(models.Model):
NameandLastName = models.CharField(max_length = 50)
...
class Consult(models.Model)
Patient = models.ForeignKey(Patient, on_delete = models.CASCADE)
simptoms = models.CharField(max_length = 50)
option_pay = models.CharField(max_length = 40, choices = pay_method)
...
Views.py
class Consult(LoginRequiredMixin, CreateView):
model = Consult
template_name = 'Manage/ConsultAdd.html'
form_class = forms.FromConsultAdd
def form_valid(self, form):
form.instance.autor = self.request.user
messages.success(self.request, f"smth")
return super().form_valid(form)
I think I have to put smth on views, but idk what.
CodePudding user response:
I think the only thing you're missing is the saving the instance, this lets Django know you updated the object.
Put this in your form_valid:
def form_valid(self, form):
form.instance.autor = self.request.user
form.instance.save()
messages.success(self.request, f"smth")
return super().form_valid(form)