User Defined Settings

28 views
Skip to first unread message

Ryan Quinn

unread,
Jul 18, 2019, 5:15:04 PM7/18/19
to Django REST framework
Is there a place for user defined settings? Stuff that isn't going to change that often, but still might need to be tweaked from time to time?

Here's the scenario. I'm creating objects, and I'm setting an expiration date X days in the future. I have a custom create function in my serializer setting the "expiration" field to the future date, and it's working well. Everything is getting set correctly.

validated_data['expiration'] = datetime.datetime.now( datetime.timezone.utc ) + datetime.timedelta( days=40 )

The problem is, the code needs to be changed if I want to raise or lower the time the objects are stored, and I would like to move this into a config file to make it easier to work with. It's a system wide setting, and the admin gets to pick how long the objects are stored.

I found the settings page in the docs, and it works fine with existing variables. It doesn't work when I add my own variables. This does not work.

settings.py:

  RET_FRAMEWORK = {
    ...
    'OBJEXP': 4,
  }

serializers.py:
  days = api_settings.OBJEXP
  validated_data['expiration'] = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=days)

What am I missing?

Thanks in advance!
Ryan

Gonzalo Amadio

unread,
Jul 18, 2019, 10:13:01 PM7/18/19
to Django REST framework
This package can solve your request.

You can define settings , and then update them on the admin. Without having to reload server


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/f4f4f62d-df06-49da-8967-94f7d7cb5faf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yago

unread,
Jul 18, 2019, 11:19:13 PM7/18/19
to django-res...@googlegroups.com

Why not to use a Mixin?

By doing that, you could override for example, the save method and call it at the end of you implementation.

I think it would be good because  of the explicity (Mixins are clearly showed on the classes top)

https://stackoverflow.com/a/547714/10243823   

Yago Gehres,

Reply all
Reply to author
Forward
0 new messages