Pass logged user to model field django -


I do not know how to get a username from the current user.

I have an edit djano-crispy-forms : The form provided with the class RecepcionForm (forms.ModelForm): fecha_recepcion = Forms.DateField (widget = dateInput ()) def __init __ (self, * Args, ** kwargs): Super (RecepcionForm, itself) .__ init __ (* args, ** kwargs) self.helper = FormHelper (self ) Self.helper.layout = layout (field ('id_proveedor', 'anio', 'mess',' usury ', redone = true), field set (' ',' fade_recpson ',' number_kirvis', submit ('save ',' Gabra ')) class meta: model = datal reciprocation

my views.py : @login_required (login_url = '/ login /') DF receptionist Weave (request): idp = request.GET .get ('i') anio = request.GET.get ('a') mes = request .GET.get ('M') if request.method == 'POST': r = DetalleRecepcion.objects.get (id_proveedor = Idp, anio = anio, mes = mes) form = reception form (request.post, example = r ) If form.is_valid (): return form.save () HttpResponseRedirect ('/ monitor /') Other: r = DetalleRecepcion.objects.get (id_proveedor = idp, anio = anio, mes = mes) form = reception form (example = R) Returns render_to_response ('recepcion.html', {'form': form}, context_instance = RequestContext (request))

I need to fill the field usuario . Before I save the form I tried it with form = request.user.username . I am confused with that the value in the form definition or in the view has to be passed.

If the value derived from the database can be overwritten and fill the field with the user name in the field.

Another question: How can I change the widget type in the form? The field id_proveedor is a foreign key and is presented as drop down box (select widget), but I need to show the value displayed in that label where the value can not be edited. I have tried with only readable assets, but the user is not able to type in the selected box but is able to select from the drop down.

"text">

You always have the necessary logic for a form class or Keywords can pass arguments, you have to remove them from * args or ** kwargs which is passed when calling super () Otherwise, DJJongo will throw an exception because it is not receiving arg or kwarg : / P>

  class MyForm (forms.ModelForm) : Def __init __ (self, * args, ** kwargs): self.user = kwargs.pop ('user') # .pop () notice (MyForm, self) .__ init __ (* args, ** kwargs) # views.py def my_view (requested): # The user is logged in as the form = myForm (user = request.user)    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -