Wednesday, February 10, 2016

Re: Django saved instance model WITH ERROR

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

On Wednesday, February 10, 2016 at 6:04:39 AM UTC-5, ylativ oknesyl wrote:

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]: 

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



--
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/ba2868aa-ec2c-42e7-94b8-32a874a94760%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment