unable to save object Django (2.0)

45 views
Skip to first unread message

harsh sharma

unread,
Jan 23, 2018, 8:10:00 PM1/23/18
to Django users
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()

Anoosha Masood Keen

unread,
Jan 24, 2018, 9:28:07 AM1/24/18
to Django users
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()

Andy

unread,
Jan 25, 2018, 9:39:43 PM1/25/18
to Django users
what is the actual error message?

Omar Abou Mrad

unread,
Jan 26, 2018, 5:26:14 AM1/26/18
to django...@googlegroups.com
On Tue, Jan 23, 2018 at 10:10 PM, harsh sharma <harshsha...@gmail.com> 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:

Your "except" swallows the exceptions that are happening and you're unable
to figure out the reason it's failing.
 
class personmanager(models.Manager):
def create_person(self,note,link,number):
pers = self.create(note=note,link=link,number=number)
return pers


The problem is due to your use of "note" whereas the field name
is actually "notes" (plural)
 
Finally, the entire "create_person" serves no purpose as you're just
aliasing "create"

person.object.create_person(a=a, b=b, c=c)
Person.objects.create(a=a, b=b, c=c)

Regards,

harsh sharma

unread,
Jan 26, 2018, 1:36:48 PM1/26/18
to Django users
change that variable but still i mm unable to save the object

harsh sharma

unread,
Jan 26, 2018, 2:08:09 PM1/26/18
to Django users
thank you the problem has being solve 

regards


On Friday, January 26, 2018 at 10:56:14 AM UTC+5:30, Omar Abou Mrad wrote:

harsh sharma

unread,
Jan 26, 2018, 2:09:49 PM1/26/18
to Django users
their is no error message 
i am doing something wrong in my forms
which is being solved
thank you

 
regards

harsh sharma

unread,
Jan 26, 2018, 2:10:48 PM1/26/18
to Django users
thank you 

regards 
Reply all
Reply to author
Forward
0 new messages