Monday, December 31, 2012

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

Hi coded,
You didn't say what the new error is. Anyway, in your view, `name` is not in the namespace. Its just like doing this:

Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
>>>a = 1
>>>a + a
2
>>>a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined

....which is exactly what you are doing with this line:

mall=Fin.objects.get(name)

In your situation, you are supposed to supply the name argument (and also the user argument. The primary key would be much better since it is unique for each record) so assuming in your form the client chose say 'Galleria' (and assuming you have a Fin object with a field name 'Galleria'), you should do something like:

mall = Fin.objects.get(name='Galleria', user=user)

Don't forget that .get() will throw an error if multiple objects fit the arguments supplied and if no object fits the arguments supplied. That said, it will be better if you supply a primary key as an argument since its very possible to have multiple Fin objects with exactly the same data contained in its fields.

Meanwhile, I can't find anywhere on your form definition that seems to be collecting 'name' for Fin.

On Mon, Dec 31, 2012 at 3:13 AM, coded kid <duffleboi911@gmail.com> wrote:
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.


--
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