[Answered ]-"user" is not set when parsing a data-dict to a Django form, but all other fields are

1๐Ÿ‘

โœ…

How come, I cannot set the user in the data-dict.

A form will ignore all fields that it does not have to process. This makes the webserver more secure: here we ensure that the user can not create a MyModel for another user.

If thus a field belongs to the excluded attribute, or not in the fields attribute from the Meta class, then the form will not set the corresponding model object attribute.

You did not specify the instance to the form:

def myView(request):
    user = request.user
    instance = MyModel(user=user)
    data = {"link":link,"date_added":domain}
    form = MyModelForm(data=data, instance=instance)
    form.save()

Leave a comment