[Answered ]-Django: How to implement system flags

1๐Ÿ‘

โœ…

Not sure from you explanation in what context you require this but I have a model which holds a number of key/value pairs used in validator checks and other things. The keys are all needed by each implementation of the project but the values will differ between projects. The values should be maintainable by an admin user. The values usually do not need to change very much once set. Given that, I decided to put them in a model. It is a bit weird but simple enough.

  1. You should be able to limit write access to the model to the one row for either your app or your users through your code.

  2. only ever reference the first row in the QuerySet

    row = MyVariables.objects.all()[0]
    

Test if there are rows first. if you think there might accidentally be more than one record then make sure it is ordered (but that should never happen if you did (1) correctly.

๐Ÿ‘คerikvw

1๐Ÿ‘

There are a couple of apps already dealing with this, check out http://djangopackages.com/grids/g/live-setting/

๐Ÿ‘คDanny W. Adair

0๐Ÿ‘

Iโ€™m also a bit confused on your goal, but Iโ€™d recommend looking at the Model Instance section of the docs. You should probably look at customizing the validation or cleaning of the model.

If your goal is to only have 1 row flagged in the table for your model: during the validation you can run a query to see if any other row is flagged, and update them to be not flagged. (or delete them).

This question Unique BooleanField value in Django may be helpful.

๐Ÿ‘คj_syk

Leave a comment