1

This is an extension from my post here preventing crud operations on django model

A short into to the problem , im currently using a package called django-river to implement a workflow system in my application. The issue is that they do not have a predefined 'start' , 'dropped' , 'completed' state. Their states are stored as a django model instance. This would mean that my application is unable to programmatically differentiate between the states. Therefore , the labels of these states has to be hardcoded into my program (Or does it? Maybe someone has a solution to this?)

Suppose that there is no solution to the issue other than hardcoding the states into my application , this would mean that i would have to prevent users from updating , or deleting these states that i have pre created initially.

My idea is to have a form of validation check within the django model's save method . This check would check that the first 3 instances of the State model is always start , deactivated and completed and in the same order. This would prevent the check from passing through whenever a user trys to change items at the ORM level.

However , it would seem that there is 2 issues with this:

  1. I believe django admin doesn't run the model class save method

  2. Someone is still able to change the states as long as the way they changed it does not pass through the save() method. AKA from the DB SQL commands

Although it is unlikely to happen , changing the name would 'break' my application and therefore i wish to be very sure that no one can edit and change these 3 predefined states.

Is there a fool proof way to do this?

neowenshun
  • 860
  • 1
  • 7
  • 21

2 Answers2

0

My idea is to have a form of validation check within the django model's save method.

if i understand your description, maybe you can just override the save() function of your model like so:

class MyModel(models.Model):

[..]

    def save(self, *args, **kwargs):

        # Put your logic here ..

        super(MyModel, self).save(*args, **kwargs)
cizario
  • 3,995
  • 3
  • 13
  • 27
  • my concern is that the some methods of creating the model instance does not pass through the save method. Lets say it was created by the django admin or by sql commands – neowenshun Jun 30 '20 at 09:58
0

I got the answer from django documentation

from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

def validate_even(value):
    if value % 2 != 0:
        raise ValidationError(
            _('%(value)s is not an even number'),
            params={'value': value},
        )

You can add this to a model field via the field’s validators argument:

from django.db import models

class MyModel(models.Model):
    even_field = models.IntegerField(validators=[validate_even])

FYI: It is not really mandatory to use gettext_lazy and you can use just message as follows

from django.core.exceptions import ValidationError

def validate_even(value):
    if value % 2 != 0:
        raise ValidationError(
            ('%(value)s is not an even number'),
            params={'value': value},
        )
Madiyor
  • 761
  • 9
  • 23
  • Since the model i am going to edit is from a third party package , i would like to check that if this method is applicable for 'proxy model' – neowenshun Jun 30 '20 at 10:12