views:

290

answers:

3

I found a custom field model (JSONField) that I would like to integrate into my Django project.

  • Where do I actually put the JSONField.py file? -- Would it reside in my Django project or would I put it in something like: /django/db/models/fields/

  • Since I assume it can be done multiple ways, would it then impact how JSONField (or any custom field for that matter) would get imported into my models.py file as well?

A: 

The best thing would be to keep Django and customizations apart. You could place the file anywhere on your pythonpath really

googletorp
+1  A: 

For the first question, I would rather not put it into django directory, because in case of upgrades you may end up loosing all of your changes. It is a general point: modifying an external piece of code will lead to increased maintenance costs.
Therefore, I would suggest you putting it into some place accessible from your pythonpath - it could be a module in your project, or directly inside the site-packages directory.

As about the second question, just "installing" it will not impact your existing models.
You have to explicitly use it, by either by adding it to all of your models that need it, either by defining a model that uses it, and from whom all of your models will inherit.

Roberto Liffredo
Oh, ok thanks - and as for my second question, I just meant would the location of the file change how I would import it? e.g.: from xx import xx
KeyboardInterrupt
If you put it in site-packages, you can simply do " import JSONField ", or "from JSONField import xxx"
Roberto Liffredo
A: 

It's worth remembering that Django is just Python, and so the same rules apply to Django customisations as they would for any other random Python library you might download. To use a bit of code, it has to be in a module somewhere on your Pythonpath, and then you can just to from foo import x.

I sometimes have a lib directory within my Django project structure, and put into it all the various things I might need to import. In this case I might put the JSONField code into a module called fields, as I might have other customised fields.

Since I know my project is already on the Pythonpath, I can just do from lib.fields import JSONField, then I can just do myfield = JSONField(options) in the model definition.

Daniel Roseman