Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this an error in the documentation?
    primarykey
    data
    text
    <p>Today I started reading the documentation for <code>django.forms</code>. The API seems easy to use and I started experimenting with it. Then I started experimenting with <code>django.forms.ModelForm</code> but I cannot really see where I went wrong.</p> <p>My problem starts here: <a href="https://docs.djangoproject.com/en/1.4/topics/forms/modelforms/#the-save-method" rel="nofollow">the save method</a> when creating a <code>form</code> with an <code>instance</code>.</p> <p>My model is</p> <pre><code>class Process(models.Model): key = models.CharField(max_length=32, default="") name = models.CharField(max_length=30) path = models.CharField(max_length=215) author = models.CharField(max_length=100) canparse = models.NullBooleanField(default=False) last_exec = models.DateTimeField(null = True) last_stop = models.DateTimeField(null = True) last_change = models.DateTimeField(null = True, auto_now=True) </code></pre> <p>and my form is</p> <pre><code>class ProcessForm(ModelForm): class Meta: model = Process fields = ('name', 'path', 'author') </code></pre> <p>I only wanted the <code>name</code>, <code>path</code> and <code>author</code> fields since the other ones are automatically set when saving the model. Anyway, in my test database I already have entries and I've chosen one whose fields are <strong>all</strong> set and valid.</p> <p>In the documentation you can read:</p> <pre><code># Create a form to edit an existing Article. &gt;&gt;&gt; a = Article.objects.get(pk=1) &gt;&gt;&gt; f = ArticleForm(instance=a) &gt;&gt;&gt; f.save() </code></pre> <p>Very well, I wanted to do the same with my own code:</p> <pre><code>&gt;&gt;&gt; from remusdb.models import Process &gt;&gt;&gt; from monitor.forms import ProcessForm &gt;&gt;&gt; &gt;&gt;&gt; proc = Process.objects.get(name="christ") &gt;&gt;&gt; pf = ProcessForm(instance=proc) &gt;&gt;&gt; pf.save() Traceback (most recent call last): File "&lt;console&gt;", line 1, in &lt;module&gt; File "/home/shaoran/devpython/lib/python2.6/site-packages/django/forms/models.py", line 364, in save fail_message, commit, construct=False) File "/home/shaoran/devpython/lib/python2.6/site-packages/django/forms/models.py", line 87, in save_instance save_m2m() File "/home/shaoran/devpython/lib/python2.6/site-packages/django/forms/models.py", line 78, in save_m2m cleaned_data = form.cleaned_data AttributeError: 'ProcessForm' object has no attribute 'cleaned_data' &gt;&gt;&gt; pf.is_bound False &gt;&gt;&gt; pf.is_valid() False </code></pre> <p>Even though <code>proc</code> is a valid <code>Process</code> object the form object doesn't seem to agree with me. If I do as the next example</p> <pre><code>&gt;&gt;&gt; post = { "name": "blabla", "path": "/somewhere", "author": "me" } &gt;&gt;&gt; pf = ProcessForm(post, instance=proc) &gt;&gt;&gt; pf.is_bound True &gt;&gt;&gt; pf.is_valid() True &gt;&gt;&gt; pf.cleaned_data {'path': u'/somewhere', 'name': u'blabla', 'author': u'me'} </code></pre> <p>then it works like in third example of the documentation.</p> <p>Am I missing something or is there an error in the documentation? Or is my <code>Model</code> code somewhat wrong?</p> <p>This is the content of <code>proc</code></p> <blockquote> <blockquote> <blockquote> <p>proc.<strong>dict</strong> {'name': u'christ', 'last_stop': datetime.datetime(2012, 10, 5, 16, 49, 13, 630040, tzinfo=), 'author': u'unkown', '_state': , 'canparse': False, 'last_exec': datetime.datetime(2012, 10, 5, 16, 49, 8, 545626, tzinfo=), 'key': u'aed72c9d46d2318b99ffba930a110610', 'path': u'/home/shaoran/projects/cascade/remusdb/test/samples/christ.cnf', 'last_change': datetime.datetime(2012, 10, 5, 16, 49, 13, 631764, tzinfo=), 'id': 5}</p> </blockquote> </blockquote> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload