[Ncr-Python.in] Django - Loading a large default value in a model

Vaidik Kapoor kapoor.vaidik at gmail.com
Wed Nov 21 07:50:02 CET 2012


On 21 November 2012 12:03, Nandeep Mali <n9986.mali at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ncr-python.in/attachments/20121121/63a4999e/attachment.html>


More information about the Ncr-Python.in mailing list