python - Django IntegrityError, get error number or code -


i want check integrityerror(1048, "column 'ean' cannot null").
proper way this? feel i'm not using best method.

product class

class product(models.model):     ean = models.bigintegerfield()     name = models.charfield() 

currently i'm doing crazy trick.

newproduct = product(ean=none, name='foo')  try:     newproduct.save() except integrityerror, e:     error = e  code = error.__getslice__(0,1) code = error[0]  # handle error 1048 

i love see proper example of handling specific integrityerror in python / django.

i think best solution not handle integrityerror validate model instance before saving it:

# run validation try:     newproduct.full_clean() except validationerror error:     error.error_dict  # dictionary mapping field names lists of validationerrors.     error.message_dict  # dictionary mapping field names lists of error messages.      # in case can check     e in error.error_dict.get('ean'):         if e.code == 'blank':             # ean empty 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -