Answer the question
In order to leave comments, you need to log in
How to validate only passed fields in ModelForm?
There is a TeamParticipant model and a TeamParticipantUpdateForm form model.
There are email, name, surname fields, all of them are required.
You need to edit the database record through the model via a POST request, but there is a problem. For example, if I want to edit only email, then the validation gives me an error that name and surname are required fields.
But the fact is that this is editing, I understand that these fields are required, but in theory they should be taken from the TeamParticipant model.
TeamParticipant Model
class TeamParticipant(models.Model):
email = models.EmailField('Email')
name = models.CharField('Имя', max_length=64)
surname = models.CharField('Фамилия', max_length=64)
class TeamParticipantUpdateForm(ModelForm):
class Meta:
model = TeamParticipant
fields = ['email', 'name', 'surname',]
request = HttpRequest()
request.POST = {
"email": "[email protected]",
}
participant = TeamParticipant.objects.get(email="[email protected]")
participant_data = TeamParticipantUpdateForm(
request.POST,
instance=participant
)
if participant_data.is_valid():
participant_data.save()
# Тут выдает ошибку, что name и surname не заполнены, хотя я передал параметр instance в TeamParticipantUpdateForm
Answer the question
In order to leave comments, you need to log in
hey, you yourself specify the fields in the TeamParticipantUpdateForm, why did you specify name and surname there if you don't need them?
The only thing that was possible was to remove the name and surname from the TeamParticipantUpdateForm, then it seems to be ok, but this is not a solutionthis is a solution, but I hope you delete it at the form description level, and not after it is created
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question