fabiocaccamo / django-extra-settings

:gear: config and manage typed extra settings using just the django admin.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validators

domeniconappo opened this issue · comments

Is your feature request related to a problem? Please describe.
In our project, we need to validate settings values when modified through the Django admin UI.
E.g. for an integer field, we would like to validate the value to be positive.

Right now the package doesn't provide positive integer field type or validators.

Describe the solution you'd like

# A solution with a validator parameter that accepts a function
months = Setting(
        name="MONTHS",
        value_type=Setting.TYPE_INT,
        value=48,
        description="Number of months.",
        validator=lambda x: x>0,
    )
months.save()

Describe alternatives you've considered

# A solution based on native datatypes 
# this can't be supported by all dbs: 
# i.e. PostgreSQL does not have an unsigned integer
months = Setting(
        name="MONTHS",
        value_type=Setting.TYPE_POSITIVE_INT,
        value=48,
        description="Number of months.",
    )
months.save()

@domeniconappo thank you for proposing this enhancement.

  • 🥉 First solution: this is not a valid solution, because the validator should be stored in the database setting row (different IntegerField settings could have different validators) and editable from the admin.

  • 🥈 Second solution: this would be perfect, if only it would be supported by all databases (this could be added anyway to avoid to use a validator when there is already database support).

  • 🥇 Third solution: add a validator field (CharField) to the Setting model storing a validator path, eg. myapp.validators.positive_int_validator. This solution would work both programmatically and using the admin. What do you think about it?

@fabiocaccamo
Thanks for your reply.
The third solution would work just fine!

I guess you could also provide some standard validators for that?

Good!

I guess you could also provide some standard validators for that?

I'm not of the idea to add some validators to this library, at least initially, but if in the future users will ask to include more validators reporting what they are using often, I will surely add validators too.


Generally I work on new features when I need them in the projects I'm working on my daily basis.
If you are in a rush / you need this feature quickly, please consider the idea to submit a PR with tests (always well appreciated) or ask your boss to sponsor my Open-Source work to make it more sustainable 💖.

Hi Fabio,
I understand of course. I would like to submit a PR for this. Let me think about (I'm definitely in a rush).

@domeniconappo thank you for the good PR.

I added also:

  • Raise ValueError is the validator path is set but invalid.
  • Add validator field in the admin model page (not in the changelist, do you think it would
    be better to have it also as editable field in the changelist?)
  • Validators section in the README.

You can safely upgrade to 0.6.0 version.

Hi @fabiocaccamo
Thank you so much! Also for latest improvements and the addition to the README.

No, I don't think the validator path would be useful in the changelist.

Thank you again. We will update to 0.6 soon.

Hi @fabiocaccamo .
Unfortunately, there is a missing migration :( My bad....
Can we reopen this? I will send a patch for it. Is that ok?
Or I create a new issue?

@domeniconappo I don't think there is a missing migration, I tested it locally without any error and also using the latest version in production.
Probably only you have this problem because you changed the max_length ?!

Anyway feel free to re-open this issue if needed.

We are also using the latest version in the last release candidate of the product :)

I've added the help_text...and it's missing in the migration file.

The problem arises during development, when you try to create a migration for a django project (python manage.py makemigrations), and Django detects the missing migration for the package. I'm using Django 3.x. Have you checked that?

It breaks the dev workflow (I receive a permission error when trying to create a new migration for our project in the dev container).

It tries to create this one (0007_alter_settings_validator). The only difference is the help_text attribute that was added after the migration was created.

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('extra_settings', '0006_setting_validator'),
    ]

    operations = [
        migrations.AlterField(
            model_name='setting',
            name='validator',
            field=models.CharField(blank=True, 
help_text="Full python path to a validator function, eg. 'myapp.mymodule.my_validator'", 
max_length=255, null=True, verbose_name='Validator'
),
        ),
    ]

Ahh ok, yes, it's really annoying the fact that django creates the migrations for help_text and verbose_name changes.

Could you submit me a PR, please?

Ahh ok, yes, it's really annoying the fact that django creates the migrations for help_text and verbose_name changes.

Yes...It's not intuitive at all and the rationale is very unclear.

Could you submit me a PR, please?

Done!

@domeniconappo you can upgrade to 0.6.1

i think there’s still a problem here:
updating from 0.5.1 to 0.6.1 i get this when running migrations:
psycopg2.errors.UndefinedColumn: column "validator" of relation "extra_settings_setting" does not exist
LINE 1: ...string", "value_text", "value_time", "value_url", "validator...

@timomeara I checked migrations and upgraded the library in a couple of online projects and everything is ok, furthermore the CI would fail since the tests run with both sqlite and postgres.

Have you run python manage.py migrate after upgrading?