M
M
maxclax2014-12-19 02:27:24
Django
maxclax, 2014-12-19 02:27:24

How to update a Django model?

Faced some misunderstanding in updating person data. There is an authorized user who edits his profile. With this whore, I slip the form on him:

@login_required
def profile(request):
    # проверяем запрос с аякса
    if request.method == "POST" and request.is_ajax():

        person_form = PersonProfileForm(request.POST)

        # проходим валидацию формы
        if person_form.is_valid():

            try:
                
                
                
                # результат операции
                data = {
                    'success': _('Профиль обновлен!')
                }

            except:
                data = {
                    'try': True
                }

        else:
            # результат операции
            data = {
                'errors': person_form.errors
            }

        # позвращаем результат в JSON
        return HttpResponse(json.dumps(data))

    else:
        # регистрируем форму
        person_form = PersonProfileForm()

    # рендеринг шаблона
    return render(request, 'person/profile.html', {'form': person_form})

This form:
class PersonProfileForm(forms.ModelForm):
    """
    Создание новой персоны
    """

    first_name = forms.CharField(label='Ваше имя', required=True)
    last_name = forms.CharField(label='Ваша фамилия', required=True)

    class Meta:
        model = Person
        fields = ['first_name', 'last_name', 'country']
        labels = {
            'country': 'От куда Вы'
        }
        help_texts = {
            'first_name': _('Обязательно указывайте все данные реальные!'),
        }

And actually the user filling it transfers the data to the handler with Ajax. They were accepted and validated. So what? :) How can I update exactly the person who is currently logged in? I shouldn't pass the user ID hidden, get a new object and update it? Is there another way out?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2014-12-19
@maxclax

stackoverflow.com/questions/8466768/using-request-...
There request.useris a current user.
If not, then you should post the model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question