[Answer]-Form.is_valid(): always returns false

1👍

You do not need the init on your model class. Also, null=False is the default. I’m not sure the text ‘admin’ will default to the user with username admin. You would need to do:

default=User.objects.get(username="admin")

You can also include the poster assignment in the creation of the object like so:

newpost = Post(post_image=request.FILES['image'], poster=request.user)

To make the form validate you need to put blank=True in the field like so:

poster = models.ForeignKey(User, blank=True, default=User.objects.get(username="admin")

I would also make the form be a ModelForm:

class AjaxImageUploadForm(forms.ModelForm):
    class Meta:
        model=Post
        exclude=['poster',]

Leave a comment