nitely / django-djconfig

:gear: Dynamic configuration made easy
http://django-djconfig.readthedocs.org
MIT License
32 stars 5 forks source link

How to save/recall config settings #16

Closed tylerecouture closed 8 years ago

tylerecouture commented 8 years ago

Thanks for the awesome app! (I think..not quite working for me yet..)

I have it installed, and can get a form with some keys and values, but they won't save? I'm just using the simple sample code you provided, but when I go back to the config form it has the initial values again instead of the updates values.

nitely commented 8 years ago

Did you register the form?

Try accessing one of the config values within the view, i.e:

from djconfig import config

def my_view(req):
    config.my_key  # This will raise AttributeError when your form is not registered
tylerecouture commented 8 years ago

Thanks! I had the wrong name when registering the form.

I tried using a ModelChoiceField and it threw an error:

class MyConfigForm(ConfigForm):

   my_first_key = forms.BooleanField(required=False)
   my_second_key = forms.IntegerField()
   my_current_semester = forms.ModelChoiceField(queryset = Semester.objects.all())

"Database is trying to update a relational field of type TextField with a value of type Semester. Make sure you are setting the correct relations"

Is this a limitation or another error on my part? Thanks for the replies!

nitely commented 8 years ago

That is a limitation.

Although, there is probably a workaround, this may work:

class MyConfigForm(ConfigForm):

    my_first_key = forms.BooleanField(required=False)
    my_second_key = forms.IntegerField()
    my_current_semester = forms.ModelChoiceField(queryset=Semester.objects.all())

    def clean_my_current_semester(self):
        # Untested, may not even work
        return self.cleaned_data['my_current_semester'].pk

Let me know if it works.

tylerecouture commented 8 years ago

It works! That's so cool! =D

nitely commented 8 years ago

Great!

I'm leaving this open so I don't forget to raise a exception if the form is not registered when calling save().