Django saved instance model WITH ERROR

16 views
Skip to first unread message

ylativ oknesyl

unread,
Feb 10, 2016, 6:04:39 AM2/10/16
to Django users

models
-----------------------

import datetime
from mimetypes import MimeTypes

from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from mutagen import mp3, mp4, oggvorbis, oggtheora

import libs

.........................

class Photo(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name=_('Name of photo'), max_length=200, help_text=_('Enter name of photo'))
    path = models.ImageField(verbose_name=_('Path to photo'), upload_to='photos', help_text=_('Choice path to photo'))
    pub_date = models.DateTimeField(verbose_name=_('Added'), auto_now_add=True)

    def recently_added(self):
        return timezone.now() >= self.pub_date >= timezone.now() - datetime.timedelta(days=3)
    recently_added.admin_order_field = 'pub_date'
    recently_added.boolean = True
    recently_added.short_description = _('Recently?')

    def get_dimension_photo(self):
        return '{0} x {1}'.format(self.path.width, self.path.height)
    get_dimension_photo.short_description = _('Dimension')

    def get_size_file(self):
        """
        documentation for function get_size_file
        """
        return libs.ReadebleSizeFile(self.path.size)
    get_size_file.short_description = _('Size')

    def get_type_file(self):
        """
        documentation for function get_size_file
        """
        mime = MimeTypes()
        mime_type = mime.guess_type(str(self.path.file))
        return mime_type[0]
    get_type_file.short_description = _('Type')

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'app_files_photo'
        get_latest_by = 'pub_date'
        ordering = ["-pub_date"]
        verbose_name = _("Photo")
        verbose_name_plural = _("Photos")

.................

shell Ipython
-----------------

In [25]: p = Photo(title='1', path='')

In [26]: p.id

In [27]: p.clean
p.clean         p.clean_fields  

In [27]: p.full_clean()
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-27-d11f1507e99a> in <module>()
----> 1 p.full_clean()

/home/wlysenko/.virtualenvs/virtual_brat/lib/python3.4/site-packages/django/db/models/base.py in full_clean(self, exclude, validate_unique)
   1134 
   1135         if errors:
-> 1136             raise ValidationError(errors)
   1137 
   1138     def clean_fields(self, exclude=None):
ValidationError: {'path': ['This field cannot be blank.']}

In [28]: p.save()

In [29]: p.id
Out[29]: 12

In [30]: 

----------------------------



Tim Graham

unread,
Feb 10, 2016, 7:57:56 AM2/10/16
to Django users
It's expected behavior. If you don't want to save an invalid instance, then don't call save() after you call full_clean() and encounter errors. As documented [1], calling save() itself don't do any validation.

[1] https://docs.djangoproject.com/en/stable/ref/models/instances/#validating-objects
Reply all
Reply to author
Forward
0 new messages