Sunday, December 30, 2012

Re: Unable to get an object value using get method in Django

Yeah, its a name of a field in my form. I want to the value
automatically since I don't know the name of the place the user will
choose. I tried adding your correction but i'm getting error.

On Dec 31, 2:33 am, Bill Freeman <ke1g...@gmail.com> wrote:
> On Sun, Dec 30, 2012 at 7:11 PM, coded kid <duffleboi...@gmail.com> wrote:
> > I want to get an object value from a model. After trying the get()
> > method, I've been unable to make it work.And I'm getting the below
> > error. I have a page that display the name of a place to users, so if
> > a user see a place he likes, he will go ahead and fill the form.And in
> > the form, I want to get the name of the place automatically from
> > another model, I don't want users to fill the name of the place. Below
> > are my codes
>
> > NameError at /welcome/
>
> >        global name 'name' is not defined
>
> > Models
>
> >  class Fin(models.Model):
> >       user=models.ForeignKey(User)
> >       name=models.CharField(max_length=100)
>
> >       def __unicode__(self):
> >           return self.user
>
> >  class Place(models.Model):
> >       user=models.ForeignKey(User)
> >       mall=models.ForeignKey(Fin)
> >       full_name=models.CharField(max_length=100)
> >       e_mail=models.EmailField(max_length=100)
> >       phone_no=models.CharField(max_length=100)
> >       pub_date=models.DateTimeField()
>
> >       def __unicode__(self):
> >           return self.full_name
> >       class Meta:
> >          ordering=['-pub_date']
>
> >   class PlaceForm(ModelForm):
> >        class Meta:
> >          model=Place
> >          fields=('full_name','e_mail','phone_no')
> >          exclude=('user','pub_date','mall')
>
> > Views:
>
> >    def place_me(request):
> >        if request.method=="POST":
> >           form=PlaceForm(request.POST)
> >           if form.is_valid():
> >              data=form.cleaned_data
> >              newbooks=Place(
> >                  user=request.user,
> >                  pub_date=datetime.datetime.now(),
> >                  mall=Fin.objects.get(name),
> >                  full_name=data['full_name'],
> >                  e_mail=data['e_mail'],
> >                  phone_no=data['phone_no'])
> >              newbooks.save()
> >              return HttpResponse('Thanks for choosing themall, we will
> > contact you as soon as possible.')
> >          else:
> >             return HttpResponse('Some fields are not filled
> > correctly')
> >      else:
> >         return render_to_response('buuk.html',
> > {'PlaceForm':PlaceForm},context_instance=RequestContext(request))
>
> > Perhaps you want:
>
>    mail=Fin.objects.get(name="something")
>
> Where I'm not sure what "something" is.  A field from your form, perhaps?
>
> Bill

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment