copy value from field1 to field2

26 views
Skip to first unread message

Feroz Ahmed

unread,
Jan 28, 2022, 2:52:23 PM1/28/22
to django...@googlegroups.com

Hi,

I am new to django,

I have field1 as dropdown with mentioned months name

filed2 is none and excluded in forms

 

I want while inserting the data , if I select month , it should insert the same value in field2

 

request your support

Ex:

field1 .value('JAN')

 

field2 is excluded from froms.py

want the value to be inserted in field2  from field 1 in db

 

update / delete /insert functions are fine, no issues  but field2 is not getting value from field 1

 

Thank You

Ammar Mohammed

unread,
Jan 28, 2022, 3:02:27 PM1/28/22
to 'Rahul Chauhan' via Django users
Hello 
You can do it in your views.py 
#If request method is get you can use 
val2 = request.GET.get('val1') 
#if method is get you can use 
val2 = request.POST.get('val1')

#then you can use val2 to in sert it in the dB 
Model.objects.create(val1=request.GET['val1'], val2=val2) 

Regards 

Ammar Mohammed
Tel: 249113075979
update / delete /insert functions are fine, no issues  but field2 is not getting value from field 1 Thank You --
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/61f44948.1c69fb81.9ac1a.ad93%40mx.google.com.

Feroz Ahmed

unread,
Jan 28, 2022, 4:13:42 PM1/28/22
to django...@googlegroups.com

Hi,

 

Thanks for your support below are the views and models,   plz guide,   no errors but no success

 

Views.py                       (cmonth is none value and excluded in form)  month is insertable from form template

def insert(request):

               form=StudentForm()

               if request.method=="POST":

                              form=StudentForm(request.POST)

                              cmonth=request.GET.get('month')

                              if form.is_valid():

                                             form.save()

                                             return redirect('/home1')

               return render(request,'enroll/insert.html',{'form':form})

------------------------------------------------------------------------------------------------------

 

Models.py

class student(models.Model):

  month = models.CharField(max_length=64, choices=COLOR_CHOICES, default='plz')

  cmonth=models.CharField(max_length=64)

  name= models.CharField(max_length=64)

  salary= models.CharField(max_length=64)

 

 

  #def student(month = request.GET['value'], cmonth=cmonth)

  def fm(request):

    fm =student.objects.create(month=request.GET['month'],cmonth=cmonth)

 

---------------------------------------------------------------------------------------------------------------------

 

cid:image001.jpg@01D814B5.8DB7AAD0

 

----------------------------------------------------------------------------------

Here I want CMonth data from Month

cid:image002.jpg@01D814B5.8DB7AAD0

 

 

 

 

 

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Bernard Mallala
Sent: 29 January 2022 01:57
To: Django users
Subject: Re: Django users

 

I am thinking about skiing this afternoon

On Friday, January 28, 2022 at 12:18:08 PM UTC-7 Kasper Laudrup wrote:

On 28/01/2022 10.12, Anil Choudhari wrote:
> How to connect hive in Django ?
>

You can't. Better find something else to do.

Kind regards,

Kasper Laudrup

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

image001.jpg
image002.jpg

Feroz Ahmed

unread,
Jan 28, 2022, 4:52:19 PM1/28/22
to django...@googlegroups.com

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Ammar Mohammed


Sent: 29 January 2022 01:32
To: 'Rahul Chauhan' via Django users
Subject: Re: copy value from field1 to field2

 

Hello 

image001.jpg
image002.jpg

Ammar Mohammed

unread,
Jan 28, 2022, 5:13:00 PM1/28/22
to 'Rahul Chauhan' via Django users
Hi 
Where is your forms.py ???

Ammar Mohammed
Tel: 0113075979
On 28 Jan 2022, 23:52 +0200, Feroz Ahmed <feroz...@gmail.com>, wrote:
Hi, Thanks for your support below are the views and models,   plz guide,   no errors but no success Views.py                       (cmonth is none value and excluded in form)  month is insertable from form templatedef insert(request):               form=StudentForm()               if request.method=="POST":                              form=StudentForm(request.POST)                              cmonth=request.GET.get('month')                              if form.is_valid():                                             form.save()                                             return redirect('/home1')               return render(request,'enroll/insert.html',{'form':form})------------------------------------------------------------------------------------------------------ Models.pyclass student(models.Model):  month = models.CharField(max_length=64, choices=COLOR_CHOICES, default='plz')  cmonth=models.CharField(max_length=64)  name= models.CharField(max_length=64)  salary= models.CharField(max_length=64)    #def student(month = request.GET['value'], cmonth=cmonth)  def fm(request):    fm =student.objects.create(month=request.GET['month'],cmonth=cmonth) --------------------------------------------------------------------------------------------------------------------- cid:image001.jpg@01D814B5.8DB7AAD0 ----------------------------------------------------------------------------------Here I want CMonth data from Month<image002.jpg>   From: django...@googlegroups.com [mailto:django...@googlegroups.comOn Behalf Of Ammar MohammedSent: 29 January 2022 01:32To: 'Rahul Chauhan' via Django usersSubject: Re: copy value from field1 to field2 

Feroz Ahmed

unread,
Jan 28, 2022, 5:16:10 PM1/28/22
to django...@googlegroups.com

Hi

 

 

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Ammar Mohammed


Sent: 29 January 2022 03:42
To: 'Rahul Chauhan' via Django users

image002.jpg
image003.jpg

Sebastian Jung

unread,
Jan 28, 2022, 6:34:11 PM1/28/22
to django...@googlegroups.com
Hello,

i think this is easy. When customer submit post data to django and form is valid. then you make following.

formentry = form.save(commit=False)

formentry.field2 = formentry.cleaned_data['field1']

formentry.save()

This is untested but i am very sure that this works....

Regards

--

Feroz Ahmed

unread,
Jan 28, 2022, 7:46:07 PM1/28/22
to django...@googlegroups.com

Hi,

I did change as per your advise,, no errors , but no changes  field 2 (cmonth)   not got value from  field1   (month) below is my views.py

 

image001.jpg

Feroz Ahmed

unread,
Jan 28, 2022, 9:17:27 PM1/28/22
to django...@googlegroups.com

Hi,

thanks for syntax code:

it works  I made added the instance the value copied from field2 (cmonth) into field1 (month)   ,, below is the updated code.

 

form.save(commit=False)

form.instance.cmonth=form.cleaned_data['month']

form.save()

 

 

From: Feroz Ahmed [mailto:feroz...@gmail.com]
Sent: 29 January 2022 06:15
To: 'django...@googlegroups.com'
Subject: RE: copy value from field1 to field2

 

Hi,

I did change as per your advise,, no errors , but no changes  field 2 (cmonth)   not got value from  field1   (month) below is my views.py

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Sebastian Jung
Sent: 29 January 2022 05:03
To: django...@googlegroups.com
Subject: Re: copy value from field1 to field2

 

Hello,

image001.jpg
Reply all
Reply to author
Forward
0 new messages