Saturday, January 2, 2016

ModelForm.is_valid() doesn't work the first time but does the second

This is running in Openshift with Python 3.3 and Django 1.8.4

Here's my model:

class Users(models.Model):
    first_nm
= models.CharField('First Name', max_length=100)
    last_nm
= models.CharField('Last Name', max_length=100)
    email
= models.CharField('Email Address', max_length=200, unique=True)

Here's my form:

class UsersForm(forms.ModelForm):
   
class Meta:
        model
= Users
        fields
= ['first_nm', 'last_nm', 'email']


When I run the shell inside Openshift, this is what happens:

Python 3.3.2 (default, Mar 20 2014, 20:25:51) 

[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux

Type "help", "copyright", "credits" or "license" for more information.

(InteractiveConsole)


>>> from django.conf import settings

>>> from package.forms import UsersForm

>>> from package.models import Users


>>> u = Users()

>>> u.first_nm = 'First'

>>> u.last_nm = 'Last'

>>> u.email = 'email@email.com'

>>> uf = UsersForm(u)

>>> uf.is_bound

True

>>> uf.is_valid

<bound method UsersForm.is_valid of <UsersForm bound=True, valid=Unknown, fields=(first_nm;last_nm;email)>>


>>> uf.save()


Traceback (most recent call last):

  File "<console>", line 1, in <module>

  File "/var/lib/openshift/5678c24389f5cff0330001cd/python/virtenv/venv/lib/python3.3/site-packages/Django-1.8.4-py3.3.egg/django/forms/models.py", line 463, in save

    construct=False)

  File "/var/lib/openshift/5678c24389f5cff0330001cd/python/virtenv/venv/lib/python3.3/site-packages/Django-1.8.4-py3.3.egg/django/forms/models.py", line 84, in save_instance

    if form.errors:

  File "/var/lib/openshift/5678c24389f5cff0330001cd/python/virtenv/venv/lib/python3.3/site-packages/Django-1.8.4-py3.3.egg/django/forms/forms.py", line 176, in errors

    self.full_clean()

  File "/var/lib/openshift/5678c24389f5cff0330001cd/python/virtenv/venv/lib/python3.3/site-packages/Django-1.8.4-py3.3.egg/django/forms/forms.py", line 392, in full_clean

    self._clean_fields()

  File "/var/lib/openshift/5678c24389f5cff0330001cd/python/virtenv/venv/lib/python3.3/site-packages/Django-1.8.4-py3.3.egg/django/forms/forms.py", line 401, in _clean_fields

    value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))

  File "/var/lib/openshift/5678c24389f5cff0330001cd/python/virtenv/venv/lib/python3.3/site-packages/Django-1.8.4-py3.3.egg/django/forms/widgets.py", line 223, in value_from_datadict

    return data.get(name, None)

AttributeError: 'Users' object has no attribute 'get'


>>> uf.save()

<Users: Users object>


First, why does uf.is_valid() return 


<bound method UsersForm.is_valid of <UsersForm bound=True, valid=Unknown, fields=(first_nm;last_nm;email)>>


the first time I call it, but on subsequent calls, uf.is_valid() returns True?


Second, why does uf.save() return that stacktrace the first time I call it, but the second time I call it, the object saves? 


Third, even though the Users object saves, the only value in the database for that row is the primary key. Neither the names nor the email field is saved.



I'm working through the Django Unchained book, and I'm also looking at the Django tutorial on djangoproject.com, and it looks to me like I'm doing everything that I'm supposed to, but obviously I'm missing something.


--Michael



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dd08dc98-e3af-49c8-b549-c5dc4e72d3d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment