Re: Add value to database without using forms in django
Yup did it.save() method. I wanted to know if we need to create form for saving or other way's are present also...I got the answer. Thanks for the reply On Saturday, September 30, 2017 at 9:19:41 PM UTC+5:30, Derek wrote: > > Yes, its a strange question. > > You can of course always add a new method to the City class: > > class City(models.Model): > >... > > def get_cities(self): > return ''.join([self.city1, self.city2, self.city3, self.city4, > self.city5])) > > and then store those in Name; but from a design POV, probably > best not to store them but just get them when you need them. > > > On Saturday, 30 September 2017 15:10:45 UTC+2, Constantine Covtushenko > wrote: >> >> Hi Mitul, >> >> Can you clarify a little bit more your question? >> What are you trying to resolve? >> You asked: "How it will be done...?" It is not clear what exactly you are >> trying to be done. >> >> I guess that it my be saving cities into model 'cityval' fields. Am I >> correct? >> >> If so you can do something like that: >> >> Name(cityval=''.join([city.city1, city.city2, ...])).save() >> >> Regards, >> Constantine C. >> >> On Sat, Sep 30, 2017 at 5:39 AM, Mitul Tyagi wrote: >> >>> I have a model named "Name" which stores the list of all cities present >>> in other model named "City". How can it be done internally in the views.py >>> without using forms. Here is the code of models.py >>> >>> " >>> # -*- coding: utf-8 -*- >>> from __future__ import unicode_literals >>> >>> from django.db import models >>> >>> >>> class Detail(models.Model): >>> username = models.CharField(max_length=100) >>> password = models.CharField(max_length=100) >>> >>> def __str__(self): >>> return str(self.username) >>> >>> >>> class City(models.Model): >>> userName = models.ForeignKey(Detail, blank=True, null=True) >>> city1 = models.CharField(max_length=100) >>> city2 = models.CharField(max_length=100) >>> city3 = models.CharField(max_length=100) >>> city4 = models.CharField(max_length=100) >>> city5 = models.CharField(max_length=100) >>> def __str__(self): >>> return "Id No:" + str(self.pk)+" and Name: "+str(self.userName) >>> >>> class Name(models.Model): >>> cityval=models.CharField(max_length=100) >>> " >>> >>> -- >>> 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 post to this group, send email to django...@googlegroups.com. >>> Visit this group at https://groups.google.com/group/django-users. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/5d83ea00-e613-4b86-830f-262b1db4ce99%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/django-users/5d83ea00-e613-4b86-830f-262b1db4ce99%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> >> -- >> Sincerely yours, >> Constantine C >> > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4406d4b0-cf38-41c6-a61d-3cad42b8bd7b%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Add value to database without using forms in django
I have a model named "Name" which stores the list of all cities present in other model named "City". How can it be done internally in the views.py without using forms. Here is the code of models.py " # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models class Detail(models.Model): username = models.CharField(max_length=100) password = models.CharField(max_length=100) def __str__(self): return str(self.username) class City(models.Model): userName = models.ForeignKey(Detail, blank=True, null=True) city1 = models.CharField(max_length=100) city2 = models.CharField(max_length=100) city3 = models.CharField(max_length=100) city4 = models.CharField(max_length=100) city5 = models.CharField(max_length=100) def __str__(self): return "Id No:" + str(self.pk)+" and Name: "+str(self.userName) class Name(models.Model): cityval=models.CharField(max_length=100) " -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5d83ea00-e613-4b86-830f-262b1db4ce99%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Updating an instance.
It is done. On Saturday, September 30, 2017 at 8:55:45 AM UTC+5:30, Mitul Tyagi wrote: > > > My form field is : > > class CityForm(forms.ModelForm): > class Meta: > model = City > fields = ['city1', 'city2', 'city3', 'city4', 'city5', 'userName'] > widgets = { > 'city1': autocomplete.Select2(url='city-autocomplete'), > 'city2': autocomplete.Select2(url='city-autocomplete'), > 'city3': autocomplete.Select2(url='city-autocomplete'), > 'city4': autocomplete.Select2(url='city-autocomplete'), > 'city5': autocomplete.Select2(url='city-autocomplete'), > } > > On Saturday, September 30, 2017 at 7:41:40 AM UTC+5:30, Constantine > Covtushenko wrote: >> >> Dear Mitui, >> >> I saw 2 questions in your inquiry: >> 1. Regarding to Form behaviour >> 2. Regarding to log message >> >> Let me clarify second: >> Provided message said that server responded with 302 - redirect response. >> It is correct as you said in your view method: >> return HttpResponseRedirect('/login/response/'+str(a.userName.username)) >> >> Now is about first. >> You printed 'using instance argument'. I asume that you are looking of a >> way add user info to POST data using form. It does not work in this way. >> That is why user info is cleared. >> >> Initial value is used to pre populate form when it is no POST data yet, >> like when you generate form on GET response. >> >> To get what you need I would suggest add user info differently. >> You have some options here. >> >> 1 - change your form - remove user field from it. You did not provide >> code of your form. But I believe that you are using user field in the form. >> 2 - add user info to POST query. It is not recommended way as this POST >> QuerySet is immutable. So to do that you need to create new QuerySet from >> previous one and put user info there. >> >> I hope that make some sense. >> >> Regards, >> Constantine C. >> >> On Fri, Sep 29, 2017 at 3:11 PM, Mitul Tyagi wrote: >> >>> >>> {% csrf_token %} >>> >>> 1st City: {{form.city1}} >>> 2nd City: {{form.city2}} >>> 3rd City: {{form.city3}} >>> 4th City: {{form.city4}} >>> 5th City: {{form.city5}} >>> >>> >>> Is the template part. >>> >>> On Saturday, September 30, 2017 at 12:16:07 AM UTC+5:30, Mitul Tyagi >>> wrote: >>>> >>>> Problem 1 : >>>> I have a model with six fields. One is userName and other five are >>>> five favourite cities.I want to update these five citiesbut when I >>>> update them using instance argument the userName becomes NONEI am >>>> using >>>> the id field to updatethe cities are updated for the id but the >>>> userName is lost. Any help for that..The code I am using is >>>> " >>>> def Update(request, city_id): >>>> if request.method == "POST": >>>> a= City.objects.get(pk= int(city_id)) >>>> print "User Updated is : ",a.userName.username >>>> >>>> f = CityForm(request.POST, instance=a, initial={'userName.': >>>> str(a.userName.username)}) >>>> f.save() >>>> return >>>> HttpResponseRedirect('/login/response/'+str(a.userName.username)) >>>> else: >>>> all_city = City.objects.all() >>>> for city in all_city: >>>> if int(city.pk) == int(city_id): >>>> a = city >>>> print "User: ",a.userName.username >>>> form_class = CityForm(initial={'userName': a.userName}) >>>> return render(request, 'formsub.html', {'form': >>>> form_class,'x':a.userName.username,'id':a.id}) >>>> " >>>> There is an error in the method also... >>>> Console prints this whenever post request is made. >>>> " [29/Sep/2017 18:43:43] "POST /login/update/18/ HTTP/1.1" 302 0 >>>> " >>>> >>> -- >>> You received this message because you are subscr
Re: Updating an instance.
My form field is : class CityForm(forms.ModelForm): class Meta: model = City fields = ['city1', 'city2', 'city3', 'city4', 'city5', 'userName'] widgets = { 'city1': autocomplete.Select2(url='city-autocomplete'), 'city2': autocomplete.Select2(url='city-autocomplete'), 'city3': autocomplete.Select2(url='city-autocomplete'), 'city4': autocomplete.Select2(url='city-autocomplete'), 'city5': autocomplete.Select2(url='city-autocomplete'), } On Saturday, September 30, 2017 at 7:41:40 AM UTC+5:30, Constantine Covtushenko wrote: > > Dear Mitui, > > I saw 2 questions in your inquiry: > 1. Regarding to Form behaviour > 2. Regarding to log message > > Let me clarify second: > Provided message said that server responded with 302 - redirect response. > It is correct as you said in your view method: > return HttpResponseRedirect('/login/response/'+str(a.userName.username)) > > Now is about first. > You printed 'using instance argument'. I asume that you are looking of a > way add user info to POST data using form. It does not work in this way. > That is why user info is cleared. > > Initial value is used to pre populate form when it is no POST data yet, > like when you generate form on GET response. > > To get what you need I would suggest add user info differently. > You have some options here. > > 1 - change your form - remove user field from it. You did not provide code > of your form. But I believe that you are using user field in the form. > 2 - add user info to POST query. It is not recommended way as this POST > QuerySet is immutable. So to do that you need to create new QuerySet from > previous one and put user info there. > > I hope that make some sense. > > Regards, > Constantine C. > > On Fri, Sep 29, 2017 at 3:11 PM, Mitul Tyagi > wrote: > >> >> {% csrf_token %} >> >> 1st City: {{form.city1}} >> 2nd City: {{form.city2}} >> 3rd City: {{form.city3}} >> 4th City: {{form.city4}} >> 5th City: {{form.city5}} >> >> >> Is the template part. >> >> On Saturday, September 30, 2017 at 12:16:07 AM UTC+5:30, Mitul Tyagi >> wrote: >>> >>> Problem 1 : >>> I have a model with six fields. One is userName and other five are five >>> favourite cities.I want to update these five citiesbut when I >>> update them using instance argument the userName becomes NONEI am using >>> the id field to updatethe cities are updated for the id but the >>> userName is lost. Any help for that..The code I am using is >>> " >>> def Update(request, city_id): >>> if request.method == "POST": >>> a= City.objects.get(pk= int(city_id)) >>> print "User Updated is : ",a.userName.username >>> >>> f = CityForm(request.POST, instance=a, initial={'userName.': >>> str(a.userName.username)}) >>> f.save() >>> return >>> HttpResponseRedirect('/login/response/'+str(a.userName.username)) >>> else: >>> all_city = City.objects.all() >>> for city in all_city: >>> if int(city.pk) == int(city_id): >>> a = city >>> print "User: ",a.userName.username >>> form_class = CityForm(initial={'userName': a.userName}) >>> return render(request, 'formsub.html', {'form': >>> form_class,'x':a.userName.username,'id':a.id}) >>> " >>> There is an error in the method also... >>> Console prints this whenever post request is made. >>> " [29/Sep/2017 18:43:43] "POST /login/update/18/ HTTP/1.1" 302 0 >>> " >>> >> -- >> 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 post to this group, send email to django...@googlegroups.com >> . >> Visit this group at https://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/0e0dc653-92fe-4c3a-9cf5-e9d2de3cb274%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/0e0dc653-92fe-4c3a-9cf5-e9d2de3cb274%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Sincerely yours, > Constantine C > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9c73c0d7-9217-4df2-8268-bab03e124103%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Updating an instance.
{% csrf_token %} 1st City: {{form.city1}} 2nd City: {{form.city2}} 3rd City: {{form.city3}} 4th City: {{form.city4}} 5th City: {{form.city5}} Is the template part. On Saturday, September 30, 2017 at 12:16:07 AM UTC+5:30, Mitul Tyagi wrote: > > Problem 1 : > I have a model with six fields. One is userName and other five are five > favourite cities.I want to update these five citiesbut when I > update them using instance argument the userName becomes NONEI am using > the id field to updatethe cities are updated for the id but the > userName is lost. Any help for that..The code I am using is > " > def Update(request, city_id): > if request.method == "POST": > a= City.objects.get(pk= int(city_id)) > print "User Updated is : ",a.userName.username > > f = CityForm(request.POST, instance=a, initial={'userName.': > str(a.userName.username)}) > f.save() > return > HttpResponseRedirect('/login/response/'+str(a.userName.username)) > else: > all_city = City.objects.all() > for city in all_city: > if int(city.pk) == int(city_id): > a = city > print "User: ",a.userName.username > form_class = CityForm(initial={'userName': a.userName}) > return render(request, 'formsub.html', {'form': > form_class,'x':a.userName.username,'id':a.id}) > " > There is an error in the method also... > Console prints this whenever post request is made. > " [29/Sep/2017 18:43:43] "POST /login/update/18/ HTTP/1.1" 302 0 > " > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e0dc653-92fe-4c3a-9cf5-e9d2de3cb274%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Updating an instance.
Problem 1 : I have a model with six fields. One is userName and other five are five favourite cities.I want to update these five citiesbut when I update them using instance argument the userName becomes NONEI am using the id field to updatethe cities are updated for the id but the userName is lost. Any help for that..The code I am using is " def Update(request, city_id): if request.method == "POST": a= City.objects.get(pk= int(city_id)) print "User Updated is : ",a.userName.username f = CityForm(request.POST, instance=a, initial={'userName.': str(a.userName.username)}) f.save() return HttpResponseRedirect('/login/response/'+str(a.userName.username)) else: all_city = City.objects.all() for city in all_city: if int(city.pk) == int(city_id): a = city print "User: ",a.userName.username form_class = CityForm(initial={'userName': a.userName}) return render(request, 'formsub.html', {'form': form_class,'x':a.userName.username,'id':a.id}) " There is an error in the method also... Console prints this whenever post request is made. " [29/Sep/2017 18:43:43] "POST /login/update/18/ HTTP/1.1" 302 0 " -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b4a138b1-871b-4c10-9c92-f863604fb071%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Autocomplete
It is done. Need to use def create(item) function to do so. Thanks for the reply. On Thursday, September 21, 2017 at 1:33:57 PM UTC+5:30, Melvyn Sopacua wrote: > > What is City1Autocomplete? Show that code, specifically what it inherits > from. > > On Tue, Sep 19, 2017 at 7:58 PM, Mitul Tyagi > wrote: > > Any help for this...Need to create new fields in case no match works for > > autocomplete > > > > > > On Tuesday, September 19, 2017 at 1:37:29 AM UTC+5:30, Mitul Tyagi > wrote: > >> > >> How to use create_field in django autocomplete. As per the doc > >> > http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#creation-of-new-choices-in-the-autocomplete-form > > >> I am getting an error while doing so... " TypeError: > City1Autocomplete() > >> received an invalid keyword 'create_field'. as_view only accepts > arguments > >> that are already attributes of the class." > >> > >> > > -- > > 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 post to this group, send email to django...@googlegroups.com > . > > Visit this group at https://groups.google.com/group/django-users. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/django-users/c34ed3e5-2817-4dc6-8d5b-1007e66f988e%40googlegroups.com. > > > > > > For more options, visit https://groups.google.com/d/optout. > > > > -- > Melvyn Sopacua > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9493cace-325b-4a05-8342-1a2987839fe3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Autocomplete
Any help for this...Need to create new fields in case no match works for autocomplete On Tuesday, September 19, 2017 at 1:37:29 AM UTC+5:30, Mitul Tyagi wrote: > > How to use create_field in django autocomplete. As per the doc > http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#creation-of-new-choices-in-the-autocomplete-form > I am getting an error while doing so... " TypeError: City1Autocomplete() > received an invalid keyword 'create_field'. as_view only accepts arguments > that are already attributes of the class." > > > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c34ed3e5-2817-4dc6-8d5b-1007e66f988e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Autocomplete
How to use create_field in django autocomplete. As per the doc http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#creation-of-new-choices-in-the-autocomplete-form I am getting an error while doing so... " TypeError: City1Autocomplete() received an invalid keyword 'create_field'. as_view only accepts arguments that are already attributes of the class." -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b1f0677d-06eb-497d-b220-1ce1b95703ca%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.