Home > database >  Django Creation View Doesn't save any data
Django Creation View Doesn't save any data

Time:11-19

My creation view redirect to blog main page well after creation but

i can't find any post's been created in the posts or admin page posts, can anyone help please

here is my view

@login_required
def add_post(request):
    if request.method == 'POST':
        post_form = PostForm(request.POST, request.FILES, instance=request.user)
        snippet_form = SnippetForm(request.POST)
        if post_form.is_valid() and snippet_form.is_valid():
            post = post_form.save(commit=False)
            snpt = snippet_form.save(commit=False)
            post.creator = request.user
            snpt.id = post.id
            post.save() and snpt.save()
            return redirect('blog:index')
    else:
        post_form = PostForm()
        snippet_form = SnippetForm()
    return render(request, 'blog/add_post.html', {'post': post_form, 'snpt': snippet_form})

what's wrong in this view cause i been able to save new post from admin add new post but from client it doesn't save anything

Do I need to use model create() method here or what?

*Any required snippet i will provide but the problem is in this snippet any help is really appreciable

enter image description here

enter image description here

enter image description here

Update:

my Post model

class Post(models.Model):
    ...
    creator = models.OneToOneField(settings.AUTH_USER_MODEL...
    snippet = models.OneToOneField(Snippet,...
    ...

CodePudding user response:

post.save() and snpt.save()

should be:

post.save()
snpt.save()

I suspect you were thinking (by using the keyword and) that you want to ensure both models are saved at the same time, but you are actually asking Python to compare the return results of the two functions to see if they are both True; this is breaking your code (as save() returns None, only your first save() is running as Python will not even execute the second as there's no way they can both be True when the first has already returned Falsey).

You may find this question useful in your scenario.

CodePudding user response:

in case anyone interested to know

Firstly As The relation between Post model and Snippet model is OneToOne, following line

snpt.id = post.id

need to be removed would violates the unique pk constraint, if you changed the relation to foreign key that would add another instance of snippet with another pk

you can remove it or assign instead

post.snippet = snpt

Secondly No need for commit=False in snpt_form as it already an instance of post_form and i didn't want to change anything else except the form fields

Thirdly Either put

creator=request.user

instead of

instance=request.user

inside the post_form

or remove it and leave

post.creator = request.user

forth Also as @bigkeefer mentioned but this is secondly arises after solving the main problem the short circuit python precedence rule causing this line to fail

post.save() and snpt.save()

instead put

post.save()

snpt.save()
  • Related