How do i auto-populate fields in django? How do i auto-populate fields in django? django django

How do i auto-populate fields in django?


Your code may look like this:

from django.contrib.auth.decorators import login_requiredclass QuestionForm(forms.ModelForm):    class Meta:         model = Question@login_requireddef ask(request):    form = QuestionForm(request.POST)    if form.is_valid():        question = form.save(False)        question.userid = request.user        question.save()    #...


This blog entry (by James Bennett) might prove useful for you as well...it lays out a way to do almost exactly what you require.


For a more recent - and likely to be updated - resource, I recommend the official Django documentation. This very example has made it's way into the ModelAdmin methods section of the ModelAdmin documentation.

If you're like me, you'll be tempted to just grab that example and run, but you might benefit from slowing down, taking a few minutes to read, and then implementing - I certainly would have...