[Answered ]-Is there a way to get my "Author" field prepopulating in my Django form?

1👍

You only provide initial data to your form when request.method == 'POST'. Instead you want to prepopulate the form, right? Therefore you need to pass initial data on 'GET'request.

[...]
if request.method == 'POST':
    form = ReviewForm(request.POST)  # no initial here
    if form.is_valid():
        review = form.save(commit=False)
        review.author = get_initial(self)
        review.media_type = media_type
        review.media_id = media_id
        if review.media_type == 'movie':
            review.movie = media
        elif review.media_type == 'game':
            review.game = media
        elif review.media_type == 'show':
            review.show = media
        review.save()
else:
    form = ReviewForm(initial={'author': request.user})  # when GET request you also want to provide initial data!
[...]

Leave a comment