Saturday, March 30, 2019

Re: Overriding settings in Django when used by the models

Hi Carlton,

1. I apologize for the time taking me to reply. I had other things to handle before I could handle this issue.

2. The settings in models such as User are per class/model and not per instance. You can see the models in the following file:

For example, you can see the following code:
    AGE_VALID_VALUES_IN_MODEL = range(settings.MIN_AGE_ALLOWED_IN_MODEL, settings.MAX_AGE_ALLOWED_IN_MODEL)
    AGE_VALID_VALUES_IN_FORMS = range(settings.MIN_AGE_ALLOWED_IN_FORMS, settings.MAX_AGE_ALLOWED_IN_FORMS)

These constants, for example, are used in the validators:


def age_is_valid_in_model(age):
    from .models import User
    return (age in User.AGE_VALID_VALUES_IN_MODEL)


def age_is_valid_in_forms(age):
    from .models import User
    return (age in User.AGE_VALID_VALUES_IN_FORMS)


And also `User.settings` appears many times in our code.

In general the model in all the project uses the same constant values, except in some tests where I want to override these values in order to test some specific cases which can't be tested with the values used in production. So I'm not sure using a property is the correct thing to do here. I'm looking for a way to define settings per class/model, but in a way that can be overridden in tests.

What do you think?

3. (Currently there is a hack I used in the tests, you can see `def _1___set_up` in https://github.com/speedy-net/speedy-net/blob/staging/speedy/core/base/test/models.py but it's a temporary hack and I don't think it's good as a permanent solution.

By the way, I tried to call the function ___set_up but it didn't work, there is a problem with functions which start with 2 or more underlines.)

4. By the way, `validators` may be a property since it's only used per instance, but I'm not sure about settings. Maybe I can use the settings differently in `validators` and define it there as a property?

On Thu, Jan 24, 2019 at 11:09 AM Carlton Gibson <carlton.gibson@gmail.com> wrote:
Hi Uri. 

Does it work if you make `User.settings` a property, that fetches the values dynamically, rather than assigning them once at import time? 

Something like: 

@property
def settings(self):
    return django_settings.USER_SETTINGS


--
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/f15c64fa-4dac-4003-a619-0a2c612e9b71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/CABD5YeE868KDtSOVCrRw-b_DUmPrOcgywvi92Mcdc2NSDW1%2B%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment