[Answered ]-How to pass a value to Django-Modelform in InputHidden charField from the another model

2👍

timezone = timezone.filter(user=user)
return timezone.timezone

timezone.filter(...) will return you a queryset, and you can not call .timezone on a queryset. You must get the related object by either using get:

timezone = timezone.get(user=user)

or by list slicing:

timezone = timezone.filter(user=user)[0]

UPDATE: Ok, i just notice it.

Problem is, you can not pass a request object to a form’s save method. Best way to deal with the problem is doing it in your view layer.

In your view:

my_form = HostCreateForm(request.POST):
    if my_form.is_valid(): #do the validation
        my_inst = my_form.save(commit=False) #pass the form values to a new model instance without saving.
        my_inst.timezone = request.user.username #set the user or do whhatever you want eith the form data.
        my_inst.save() #that will call your save methhod that you override.

And you remove the parts that sets the user from your save method.

👤Mp0int

Leave a comment