Parameter passed in save() difficult to understand
<p>I am learning Django from a most recommended and beneficial book named 'Django By Example'. There is a project called Bookmark. I am now stuck in the forms part which is for downloading the image and saving image object to the database. I could understand validation part(clean_url) and also downloading part. I could not get into the parameter passed in to save()</p>

<blockquote>
  <p>save(self, force_insert=False, force_update=False, commit=True)</p>
</blockquote>

<p>and saving image object </p>

<blockquote>
  <p>image.image.save(image_name, ContentFile(response.read()), save=False)</p>
</blockquote>

<p>Where is force_insert and force_update been used in this function?</p>

<p>Also i did not understand the parameter part in image.image.save() because image has field like title, url, description, image etc. What image_name is refering to? I think response.read() part is for image field.</p>

<p>Could anyone please make me clear?</p>

<p><strong>Here is the code</strong> </p>

<pre><code>class Image(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             related_name='images_created')
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, blank=True)
    url = models.URLField()
    image = models.ImageField(upload_to='images/%Y/%m/%d')
    description = models.TextField(blank=True)
    created = models.DateTimeField(auto_now_add=True,
                                   db_index=True)
    users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,
                                        related_name='images_liked',
                                        blank=True)
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>def image_create(request):
    """
    View for creating an Image using the JavaScript Bookmarklet.
    """
    if request.method == 'POST':
        # form is sent
        form = ImageCreateForm(data=request.POST)
        if form.is_valid():
            # form data is valid
            cd = form.cleaned_data
            new_item = form.save(commit=False)
            # assign current user to the item
            new_item.user = request.user
            new_item.save()
            messages.success(request, 'Image added successfully')
            # redirect to new created item detail view
            return redirect(new_item.get_absolute_url())
    else:
        # build form with data provided by the bookmarklet via GET
        form = ImageCreateForm(data=request.GET)

    return render(request, 'images/image/create.html', {'section': 'images',
                                                        'form': form})
</code></pre>

<p><strong>forms.py</strong></p>

<pre><code>class ImageCreateForm(forms.ModelForm):

    class Meta:
        model = Image
        fields = ('title', 'url', 'description')
        widgets = {
            'url': forms.HiddenInput,
        }

    def clean_url(self):
        url = self.cleaned_data['url']
        valid_extensions = ['jpg', 'jpeg']
        extension = url.rsplit('.', 1)[1].lower()
        if extension not in valid_extensions:
            raise forms.ValidationError('The given URL does not match valid image extensions.')
        return url

    def save(self, force_insert=False, force_update=False, commit=True):
        image = super(ImageCreateForm, self).save(commit=False)
        image_url = self.cleaned_data['url']
        image_name = '{}.{}'.format(slugify(image.title),
                                    image_url.rsplit('.', 1)[1].lower())

        # download image from the given URL
        response = request.urlopen(image_url)
        image.image.save(image_name,
                         ContentFile(response.read()),
                         save=False)
        print('image',image)

        if commit:
            image.save()
        return image
</code></pre>

<p><strong>Update</strong></p>

<p>To create an image i have to use this url localhost:8000/images/create/?title=title for image&amp;url=<a href="http://www.demo.com/image/image.jpg" rel="nofollow">http://www.demo.com/image/image.jpg</a> </p>

<pre><code>image = super(ImageCreateForm, self).save(commit=False)
</code></pre>

<p>In this statement we are assigning super function to a variable image.</p>

<p>super method is called with ImageCreateForm. Then whatever is returned with super() method, we call the save() method of forms.ModelForm with commit=False.</p>

<p>Now if you look in your model Image you will find "image" attribute which is of of type models.ImageField</p>

<p>For the purpose of understanding, let me rename "image" attribute of model as "image_of_model" </p>

<p>The ImageField has a method save of the form</p>

<pre><code>save(name,content, save)

image.image.save(image_name,
                     ContentFile(response.read()),
                     save=False)
</code></pre>

<p>As per my given terminology the above statement becomes.</p>

<pre><code>image.image_of_model.save(image_name,
                     ContentFile(response.read()),
                     save=False)
</code></pre>

<p>I hope this sorts out your confusion.</p>

python django python-3.x django-models django-forms