Friday, January 26, 2018

Re: unable to save object Django (2.0)

thank you 

regards 

On Wednesday, January 24, 2018 at 2:58:07 PM UTC+5:30, Anoosha Masood Keen wrote:
Try this in models.py file

class personmanager(models.Model):
    notes = models.TextField()
    link = models.URLField()
    number = models.IntegerField()
   
    def create_person(self,note,link,number):
        self.notes=note
        self.link=link
        self.number=number
        return self
    def __str__(self):
        return self.notes

object = personmanager()
a=object.create_person("this","c.com",1);
a.save()


On Tuesday, January 23, 2018 at 8:10:00 PM UTC, harsh sharma wrote:
i am trying to save the basic information about a person 
i have created a model form for it but i am unable to save the object (unable to update the database)

here is my views file
@login_required
def dashboard(request):
if request.method=="post":
form = linkform(request.POST)
if form.is_valid():
try:
notes = form.cleaned_data['note']
link = form.cleaned_data['link']
number = form.cleaned_data['number']
perso = person.object.create_person(notes,link,number)
perso.save()
return HttpResponse('succeess')
except:
return HttpResponse('can not create object')
else:
return HttpResponse('bad form')
else:
form = linkform(request.POST)
return render(request, 'dashboard.html', {'form':form})

here is my model file :
class personmanager(models.Manager):
def create_person(self,note,link,number):
pers = self.create(note=note,link=link,number=number)
return pers



class person(models.Model):
notes = models.TextField()
link = models.URLField()
number = models.IntegerField()
object = personmanager()

--
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+unsubscribe@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/6e137774-e270-4673-b36f-a9102bccc29b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment