Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method" rel="nofollow noreferrer">save()</a> should return the newly created instance.</p> <pre><code>if contractor_form.is_valid(): instance = contractor_form.save() parameters['contractor'] = instance </code></pre> <p>where id would be <code>instance.id</code>, or even better <a href="https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#the-pk-property" rel="nofollow noreferrer"><code>instance.pk</code></a>.</p> <p><a href="https://stackoverflow.com/questions/2165865/django-queries-id-vs-pk"><code>pk</code> vs. <code>id</code></a>:</p> <blockquote> <p>Regardless of whether you define a primary key field yourself, or let Django supply one for you, each model will have a property called pk. It behaves like a normal attribute on the model, but is actually an alias for whichever attribute is the primary key field for the model. You can read and set this value, just as you would for any other attribute, and it will update the correct field in the model.</p> </blockquote> <p><strong><em>Follow-up on comment:</em></strong></p> <p>Well it does work by default, so there must be something else wrong.</p> <p><strong>models.py</strong></p> <pre><code>class Category(models.Model): name = models.CharField(max_length=70) slug = models.SlugField() </code></pre> <p><strong>forms.py</strong></p> <pre><code>from django import forms from models import Category class MyModelForm(forms.ModelForm): class Meta: model = Category </code></pre> <p><strong>Test in shell:</strong></p> <pre><code>In [3]: from katalog.forms import MyModelForm In [4]: data = {'name':'Test', 'slug':'test'} In [5]: form = MyModelForm(data) In [6]: instance = form.save() In [7]: instance Out[7]: &lt;Category: Test&gt; In [8]: instance.id Out[8]: 5L </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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