ModelForm isnt working

can you tell me the exact place of the example which should help me in the docs that you talk about. All I can find is the code to set the value of a normal char in the views.


# Create a form instance with POST data.
>>> f = AuthorForm(request.POST)

# Create, but don't save the new author instance.
>>> new_author = f.save(commit=False)

# Modify the author in some way.
>>> new_author.some_field = 'some_value'

# Save the new instance.
>>> new_author.save()

If that’s the line that you’re questioning, it’s ok. There is nothing fundamentally different about the assignment of an id vs the assignment of a character field.

However, that example does not come from either of the links I referenced at ModelForm isnt working - #76 by KenWhitesell - looking at the very first example blocks at Creating forms from models | Django documentation | Django
and Creating forms from models | Django documentation | Django

yeah I know, but my form hasnt a UserField so what should I set then.

What account object do you wish to edit? (What information do you have in your view that identifies which row you wish to work with?)

>>> article = Article.objects.get(pk=1)
>>> form = ArticleForm(instance=article)

this looks good, but do I know which primary key my object has, so that I know which I should edit

You don’t have to get by primary key. (It’s an example, not a requirement or limitation.)
You can use the get method with any valid query, as long as it retrieves one and only one row.

I did it, it works :slight_smile:

Thanks a lot

@login_required(login_url='home:login')
def AccountInfoPrename(request):
        if request.method == 'POST':
            form = AccountInfoPrenameForm(request.POST)
            if form.is_valid():
                article = Account.objects.get(user=request.user)
                form = AccountInfoPrenameForm(request.POST, instance=article)
                form.save()

                

            return redirect('home:profile')
        else:
            form = AccountInfoPrenameForm()
        return render(request,
                  'home/profile.html',
                  {'form': form}

                  )