alexkisielewicz / photo-adventures

Photo Adventures Website - Portfolio Project #4 (Full-Stack Toolkit) for Diploma in Full Stack Software Development at Code Institute.
https://photo-adventures.herokuapp.com/
3 stars 1 forks source link

[BUG] Tags are not saved in the add_post and edit_post forms #27

Closed alexkisielewicz closed 1 year ago

alexkisielewicz commented 1 year ago

Describe the bug When the user sends the form with a new post or edits an existing post draft, the tag field stays empty after saving. The database is not being updated as expected.

To Reproduce Steps to reproduce the behavior:

  1. As a logged-in user go to the dashboard and click 'Add new post'
  2. Complete the form and click 'send'.
  3. As an admin, check post details in the admin panel to see that field 'tags' is empty

Expected behavior Tags should be saved in the database upon form submission on the add_post and edit_post pages

alexkisielewicz commented 1 year ago

Bug fixed by adding save_m2m method within if statement:

if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            content = post.content
            post.save()
            form.save_m2m()
            return redirect('user_account')

Solution found on: https://stackoverflow.com/questions/7083152/is-save-m2m-required-in-the-django-forms-save-method-when-commit-false https://docs.djangoproject.com/en/4.1/topics/forms/modelforms/#the-save-method