An almost reusable app, how to make it reusable?

38 views
Skip to first unread message

Victor Porton

unread,
Oct 3, 2016, 3:06:01 PM10/3/16
to Django users
I've created an app which is "almost" reusable: it depends on a variable in <PROJ>/setting.py of our project <PROJ>.

from <PROJ>.settings import TRANSACTION_ATTEMPT_COUNT

Can it be made into an reusable app?

ludovic coues

unread,
Oct 3, 2016, 3:10:18 PM10/3/16
to django...@googlegroups.com
Provide a default value or raise a ImproperlyConfigured error if the
key is missing. Document the need to set the key.

That should be enough to call your app "reusable".
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/fefb507d-c728-4f74-a3d1-0292a5e65488%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Tim Graham

unread,
Oct 3, 2016, 3:10:25 PM10/3/16
to Django users
Typically, you import settings (even those that you define yourself) using "from django.conf import settings".

https://docs.djangoproject.com/en/stable/topics/settings/#using-settings-in-python-code

Carl Meyer

unread,
Oct 3, 2016, 3:11:35 PM10/3/16
to django...@googlegroups.com
Hi Victor,
Certainly. Change the import to `from django.conf import settings`, so
you import the settings from Django rather than from your project (this
is generally the best way to access settings anyway). Then establish
some reasonable default value for the setting, and access it like
`getattr(settings, 'TRANSACTION_ATTEMPT_COUNT', default_value)`. Then
your app can work in any project, even if that project doesn't set a
value for that setting.

If there is no reasonable default value, and you want to force any
project using your app to set some value explicitly, you can also just
raise an error if the setting doesn't exist and in the exception message
instruct the developer that they must set the setting.

Carl

signature.asc
Reply all
Reply to author
Forward
0 new messages