0

HI I am updating an existing record, but everytime I do, its is saying Truncated incorrect DOUBLE value: 'user1' I am pasting my view and models here. Please help me

Model

class BasicDetails(models.Model):
   username = models.OneToOneField(User)
   name = models.CharField(max_length = 100, verbose_name = "Name")
   sex = models.CharField(max_length = 10, verbose_name = "Sex", choices = GENDER_CHOICES)
   dob = models.DateField(verbose_name = "Date of Birth")

   class Meta:
      db_table = u'wed_basicdetails'
   def __unicode__(self):
         return u'%s' % (self.username)

View

def editbasicdetails(request):
  if request.method == 'POST':
      username = User.objects.filter(username = request.user)
      instance = get_object_or_404(User, username = username)
      form = EditBasicDetailsForm(request.POST,instance=instance)
      if form.is_valid():
          form.save()
      return HttpResponseRedirect('/myprofile/')
  else:
      form = EditBasicDetailsForm(request.user)
  return shortcuts.render_to_response('editbasicdetails.html',locals(),
                                    context_instance = context.RequestContext(request))

Model Form

class EditBasicDetailsForm(forms.ModelForm):
  def __init__(self,user, *args, **kwargs):
      super(EditBasicDetailsForm, self).__init__(*args, **kwargs)

      userid = User.objects.filter(username = user).values('id')[0].get('id')
      self.fields['username'] = forms.CharField(initial = user,
                                              widget = forms.TextInput(attrs = {'readonly':'readonly'}))
  class Meta:
      model = BasicDetails

In the above view, I am not passing the username I am just getting the username value from request.user I am not sure what I am making wrong on this simple functionality.

-Vikram

vkrams
  • 7,267
  • 17
  • 79
  • 129

1 Answers1

1

The error is probably related to the inconsistent behavior of the form wrt model. Since, the database stores the OneToOneField as integer and CharField as varchar.

You don't have to override the form init method. You could do it directly with the initial argument.

form = EditBasicDetailsForm(initial = {'username' : request.user)

Update:

In your view code I would suggest you to properly intend you HttpResponseRedirect statement. Otherwise, if the form is in valid it won't display it again.

if form.is_valid():
          form.save()
          return HttpResponseRedirect('/myprofile/')
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
  • Hi Raunak, Do u want me to change the form? and I have added the above line in the if condition but the error still persists – vkrams Dec 09 '12 at 06:03
  • Yeah can you remove the __init__ method if not and try again! – Raunak Agarwal Dec 09 '12 at 06:04
  • I am using the init method in order retrieve the existing values from the database. In the above question, I have posted only the username in the form but I have all the fields `self.fields['fieldname']` – vkrams Dec 09 '12 at 06:06
  • But you don't have to if you use the initial argument its going to take care of that. Moreover, the object which you are trying to fetch from the database would be the same as of the request as in the Middleware takes care of that. – Raunak Agarwal Dec 09 '12 at 06:08
  • Can you please show us the complete error along with the location of error? – Raunak Agarwal Dec 09 '12 at 06:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20827/discussion-between-raunak-agarwal-and-vikram) – Raunak Agarwal Dec 09 '12 at 06:27
  • After following this post http://stackoverflow.com/questions/1202839/get-request-data-in-django-form I got the solution – vkrams Dec 12 '12 at 14:14