django-preferences icon indicating copy to clipboard operation
django-preferences copied to clipboard

Programatiaclly Set Preferences

Open unitedsoftwork opened this issue 5 years ago • 1 comments

I'm writing some tests that use the Preferences app.

In the setup function of the test class, i have this code.

from preferences import preferences preferences.Test.ip = "http://192.168.1.7/" preferences.Test.save()

yet when i assign the ip to the field, the field is still blank. This occurs even before the save.

Is there a way to set up preferences in this manner? Specifically concerning tests.

unitedsoftwork avatar Aug 14 '19 14:08 unitedsoftwork

Did you ever figure this out? I'm also trying to figure out how to update a preference from a script outside of the admin view.

Edit: Just figured this out. After going through models, it looks like the preferences.Test (in your case) is just a convenience function to access the data in the fields, not an actual manager or queryset object. You need to import the model directly (instead of using preferences) and then you can pass it the pk from preferences to get what you need.

So you would do something like this:

from preferences import preferences from myapp.models import Test

x = Test.objects.get(pk=preferences.Test.pk) x.ip = "192.168.1.7" x.save()

jkadin avatar Jun 24 '20 01:06 jkadin