D
D
Dmitry Baibukhtin2021-01-02 00:33:13
Django
Dmitry Baibukhtin, 2021-01-02 00:33:13

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)


TeamParticipantUpdateForm model
class TeamParticipantUpdateForm(ModelForm):
    class Meta:
        model = TeamParticipant
        fields = ['email', 'name', 'surname',]


Scenario
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


In total, how to make it so that you can only send email in POST, and only it is updated in the database when you save. The only thing that was possible was to remove name and surname from TeamParticipantUpdateForm, then it seems to be fine, but this is not a solution.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2021-01-02
@PiloTeZ

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 solution
this is a solution, but I hope you delete it at the form description level, and not after it is created

A
axblue, 2015-09-12
@Huf

Вообще-то, всё работает

setTimeout(function(){$('.loader-inner').fadeOut('fast')},10000); //10000 = 10 секунд

codepen.io/anon/pen/meegrE
Подозреваю что ты просто забыл подключить библиотеку:)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question