Re: error while submitting data in the form django

2011-02-08 Thread Tom Evans
On Tue, Feb 8, 2011 at 3:37 PM, sushanth Reddy  wrote:
> I am trying to insert data into db it throws below error,can any please fix
> this.

form.Fields normalize to a python value. That python value must be
assignable to the model field you are trying to update. You cannot
assign a string to a foreign key field, it expects an instance of the
object that it is a foreign key for.

The solution is to use field that normalizes to the correct value, in
this case a ModelChoiceField. If you don't want it to appear, you can
still use a HiddenInput widget, but it must be a ModelChoiceField if
you want to assign it to a foreign key field.

Cheers

Tom

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



error while submitting data in the form django

2011-02-08 Thread sushanth Reddy
I am trying to insert data into db it throws below error,can any please fix 
this.


model.py

from django.db import models

# Create your models here.

class Profile(models.Model):
 name = models.CharField(max_length=50, primary_key=True)
 assign = models.CharField(max_length=50)
 doj = models.DateField()

 class Meta:
db_table= 'profile'


 def __unicode__(self):

   return  u'%s' % (self.name)



class working(models.Model):
   w_name =models.ForeignKey(Profile, db_column='w_name')
   monday =  models.IntegerField(null=True, db_column='monday', blank=True)
   tuesday =  models.IntegerField(null=True, db_column='tuesday', blank=True)
   wednesday =  models.IntegerField(null=True, db_column='wednesday', 
blank=True)

   class Meta:
db_table = 'working'



   def __unicode__(self):
  return  u'%s ' % ( self.w_name)

forms.py

from models import *
from django import forms

class WorkingForm(forms.ModelForm):

  def __init__(self,  *args, **kwargs):
super(WorkingForm, self).__init__(*args, **kwargs)
self.fields['w_name'] 
=forms.CharField(initial='x',widget=forms.HiddenInput())

  class Meta:
model = working
exclude = ('id')

view.py

# Create your views here.
from forms import *
from django import http
from django.shortcuts import render_to_response, get_object_or_404


def index(request):
  if request.method == "POST":
  form = WorkingForm(request.POST)
  if form.is_valid():
form.save()
return http.HttpResponse('Added')
  else:  
form = WorkingForm()
  return render_to_response('add_workingdays.html',locals())

Error Message:

ValueError at /

Cannot assign "u'x'": "working.w_name" must be a "Profile" instance.

Request Method: POST
Request URL:http://127.0.0.1:8000/
Django Version: 1.2.4
Exception Type: ValueError
Exception Value:

Cannot assign "u'x'": "working.w_name" must be a "Profile" instance.

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 1.2.4
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'check']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File 
"/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/core/handlers/base.py"
 in get_response
  100. response = callback(request, *callback_args, 
**callback_kwargs)
File "/home/xyz/newapp/../newapp/check/views.py" in index
  10.   if form.is_valid():
File 
"/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/forms/forms.py"
 in is_valid
  121. return self.is_bound and not bool(self.errors)
File 
"/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/forms/forms.py"
 in _get_errors
  112. self.full_clean()
File 
"/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/forms/forms.py"
 in full_clean
  269. self._post_clean()
File 
"/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/forms/models.py"
 in _post_clean
  320. self.instance = construct_instance(self, self.instance, 
opts.fields, opts.exclude)
File 
"/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/forms/models.py"
 in construct_instance
  51. f.save_form_data(instance, cleaned_data[f.name])
File 
"/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/fields/__init__.py"
 in save_form_data
  416. setattr(instance, self.name, data)
File 
"/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/fields/related.py"
 in __set__
  318.  self.field.name, 
self.field.rel.to._meta.object_name))

Exception Type: ValueError at /
Exception Value: Cannot assign "u'x'": "working.w_name" must be a "Profile" 
instance.


I did a foreign key relation between two tables and i am trying to insert 
the data to working table it get Cannot assign "u'x'": "working.w_name" must 
be a "Profile" instance.

if remove def *init*(self, *args, **kwargs): super(WorkingForm, 
self).*init*(*args, 
**kwargs) self.fields['w_name'] 
=forms.CharField(initial='x',widget=forms.HiddenInput())

it works normally,but i want to enter the username as hidden,but it throwing 
foreign key error



-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.