[Ncr-Python.in] Django - Loading a large default value in a model
I asked this on Stack Overflow once. But am wondering if people here have a better approach. I have a field called schema in a Django model that usually contains a rather large json string. There is a default value (around 2000 characters) that I would like added when any new instance is created. I find it rather unclean to dump the whole thing in the models.py in a variable. What is the best way to load this default value in my schema (which is a TextField)? Example: class LevelSchema(models.Model): user = models.ForeignKey(to=User) active = models.BooleanField(default=False) schema = models.TextField() # Need a default value for this I thought about this a bit. If I am using a json file to store the default value somewhere, what is the best place to put it? Obviously it is preferable if it is in the same app in a folder. Approaches I have been suggested: a. Set the default in the model field to a callable (method) which reads a file and return its contents. b. Do the above on post_save hook. What do you folks do? --- Nandeep
On 21 November 2012 12:03, Nandeep Mali <n9986.mali@gmail.com> wrote: [...]
I have a field called schema in a Django model that usually contains a rather large json string. There is a default value (around 2000 characters) that I would like added when any new instance is created.
I find it rather unclean to dump the whole thing in the models.py in a variable. What is the best way to load this default value in my schema (which is a TextField)? [...] Approaches I have been suggested:
a. Set the default in the model field to a callable (method) which reads a file and return its contents. b. Do the above on post_save hook.
(a) seems fine, but aren't you over-analysing this? If the default value is fixed, why not put it into a string in say, constants.py in the same directory as models.py, and just import the string in models.py, Regards, Gora
On Wed, Nov 21, 2012 at 12:14 PM, Gora Mohanty <gora@mimirtech.com> wrote: [...]
(a) seems fine, but aren't you over-analysing this? If the default value is fixed, why not put it into a string in say, constants.py in the same directory as models.py, and just import the string in models.py,
I did not mention the entire use case. As I said, the said JSON string is properly formatted and indented. I need this portable and editable in the future, a schema which will evolve over time. This same JSON string is also being used outside the scope of the Django project. As I said before, I personally find it cumbersome to dump a several hundred line long JSON string in a Python variable which is exactly not what I wanted to do.
On 21 November 2012 12:19, Nandeep Mali <n9986.mali@gmail.com> wrote:
On Wed, Nov 21, 2012 at 12:14 PM, Gora Mohanty <gora@mimirtech.com> wrote: [...]
(a) seems fine, but aren't you over-analysing this? If the default value is fixed, why not put it into a string in say, constants.py in the same directory as models.py, and just import the string in models.py,
I did not mention the entire use case. As I said, the said JSON string is properly formatted and indented. I need this portable and editable in the future, a schema which will evolve over time. This same JSON string is also being used outside the scope of the Django project.
In that case, having a file or a web service that provides the string would make sense. Reading from a file is hardly different than having a fixed string, IMHO. In both cases, source code version control systems should take care of changes in the fixed string.
As I said before, I personally find it cumbersome to dump a several hundred line long JSON string in a Python variable which is exactly not what I wanted to do.
If you are comfortable with reading it from a file, I do not really see what distinction you are making here. Regards, Gora
On 21 November 2012 12:27, Gora Mohanty <gora@mimirtech.com> wrote:
On 21 November 2012 12:19, Nandeep Mali <n9986.mali@gmail.com> wrote:
On Wed, Nov 21, 2012 at 12:14 PM, Gora Mohanty <gora@mimirtech.com> wrote: [...]
(a) seems fine, but aren't you over-analysing this? If the default value is fixed, why not put it into a string in say, constants.py in the same directory as models.py, and just import the string in models.py,
I did not mention the entire use case. As I said, the said JSON string is properly formatted and indented. I need this portable and editable in the future, a schema which will evolve over time. This same JSON string is also being used outside the scope of the Django project.
I will perhaps not indent and format and then store it in the DB. I would want it be stored in the most easily readable format for any other projects/sub-projects that may use this JSON - perhaps, indentation and formatting can be an issue here.
In that case, having a file or a web service that provides the string would make sense. Reading from a file is hardly different than having a fixed string, IMHO. In both cases, source code version control systems should take care of changes in the fixed string.
As
I said before, I personally find it cumbersome to dump a several hundred line long JSON string in a Python variable which is exactly not what I wanted to do.
If you are comfortable with reading it from a file, I do not really see what distinction you are making here.
+1 How does it really matter if it lives in a variable or not? It will be short-lived anyways.
Regards, Gora _______________________________________________ http://mail.python.org/mailman/listinfo/ncr-python.in Mailing list guidelines : http://lug-iitd.org/Mailing_List_Guidelines
- Vaidik
On 21 November 2012 12:03, Nandeep Mali <n9986.mali@gmail.com> wrote:
I asked this on Stack Overflow once. But am wondering if people here have a better approach.
I have a field called schema in a Django model that usually contains a rather large json string. There is a default value (around 2000 characters) that I would like added when any new instance is created.
I find it rather unclean to dump the whole thing in the models.py in a variable. What is the best way to load this default value in my schema (which is a TextField)?
Example:
class LevelSchema(models.Model): user = models.ForeignKey(to=User) active = models.BooleanField(default=False) schema = models.TextField() # Need a default value for this
I thought about this a bit. If I am using a json file to store the default value somewhere, what is the best place to put it? Obviously it is preferable if it is in the same app in a folder.
If you have many json files like that, then perhaps create a folder and have all of them there for clarity sake. Otherwise, I think it doesn't really matter where your JSON string is coming from if your model definition is clear enough so that whenever you re-visit it later, you don't find it difficult to understand. You may also consider having a dict instead of having your json stored as a string - you can import your dict from whichever module it lives in and then dump it as a default value.
Approaches I have been suggested:
a. Set the default in the model field to a callable (method) which reads a file and return its contents. b. Do the above on post_save hook.
I wouldn't do the post_save hook, perhaps. Default value using callable sounds more intuitive to me.
What do you folks do?
--- Nandeep _______________________________________________ http://mail.python.org/mailman/listinfo/ncr-python.in Mailing list guidelines : http://lug-iitd.org/Mailing_List_Guidelines
participants (3)
-
Gora Mohanty -
Nandeep Mali -
Vaidik Kapoor