Login

Tag "models"

Snippet List

Models with database views

This example shows, how to use database views with django models. NewestArticle models contains 100 newest Articles. Remember, that NewestArticle model is read-only. Tested with mysql.

  • sql
  • models
  • views
  • view
  • model
  • mysql
  • database
Read More

PowerDNS models

With these models, you can use your Django's database directly as backend for PowerDNS server (tested with MySQL). License: GNU GPL. More info: http://downloads.powerdns.com/documentation/html/configuring-db-connection.html#CONFIGURING-MYSQL

  • models
  • model
  • powerdns
  • dns
Read More

Composite Indexing for MySQL

A quick and dirty hack for composite indexing if you need it. Drop this into a models.py or some other place where it'll be loaded along with the rest of Django on start up. Then add an _index_together tuple specifying the fields you want a composite index on.

  • models
  • mysql
  • composite-indexing
Read More

inspectdb fixer

This snippet parses the output file of inspectdb and does some alterations. Mostly useful for people who regenerates models from constantly changing legacy databases. The snippet will: *Add quotes around foreign key classes, so the ordering is not significant *Append a related_name property to each foreign key with the value model class name + db_column name to evade collisions in reverse queries like: example.model: Reverse query name for field 'foreignkey' clashes with related field 'model2.foreignkey'. Add a related_name argument to the definition for 'foreignkey'. There's a slight performance degradation with using quotes class name instead of passing the class though.

  • models
  • inspectdb
  • database-import
Read More

Generic Permissions

A mixin to define permissions on models. This is more of an abstract model to subclass/customise than a plug-in solution. Explanations are [here](http://www.muhuk.com/2009/05/django-permission-system/).

  • models
  • model
  • permission
  • authorization
Read More

JSONField

This is a custom field that lets you easily store JSON data in one of your model fields. This is updated to work with Django 1.1. **Example: (models.py)** from django.db import models import JSONField class MyModel(models.Model): info = JSONField() ** Example: (shell)** >>> obj = MyModel.objects.all()[0] >>> type(obj.info) <type 'NoneType'> >>> obj.info = {"test": [1, 2, 3]} >>> obj.save() **[Code at GitHub](http://github.com/bradjasper/django-jsonfield/tree/master)**

  • models
  • fields
  • model
  • json
  • db
  • field
  • json-field
  • jsonfield
Read More

JSON Encoder for models

A simplejson encoder that knows how to encode django models, and it's little brother that also know how to deals with lazy translations.

  • models
  • json
  • encoding
Read More

Full-Text Searchable Models

A drop-in module to allow for full-text searchable models with very little effort. Tested with PostgreSQL 8.3, but should work on earlier versions with the tsearch2 module installed.

  • models
  • search
  • full-text
  • tsearch2
Read More

jQuery color picker model field

This uses the Really Simple Color Picker in jQuery: http://www.web2media.net/laktek/2008/10/27/really-simple-color-picker-in-jquery/ Get source from there or GitHub: http://github.com/laktek/really-simple-color-picker/tree/master

  • models
  • admin
  • jquery
  • widgets
Read More

ImageWithThumbsField

Easy way to generate image thumbnails for your models. Works with any Storage Backend. From: [http://code.google.com/p/django-thumbs/](http://code.google.com/p/django-thumbs/) **Usage example:** ============== photo = ImageWithThumbsField(upload_to='images', sizes=((125,125),(300,200),) To retrieve image URL, exactly the same way as with ImageField: my_object.photo.url To retrieve thumbnails URL's just add the size to it: my_object.photo.url_125x125 my_object.photo.url_300x200 Note: The 'sizes' attribute is not required. If you don't provide it, ImageWithThumbsField will act as a normal ImageField **How it works:** ============= For each size in the 'sizes' atribute of the field it generates a thumbnail with that size and stores it following this format: available_filename.[width]x[height].extension Where 'available_filename' is the available filename returned by the storage backend for saving the original file. Following the usage example above: For storing a file called "photo.jpg" it saves: photo.jpg (original file) photo.125x125.jpg (first thumbnail) photo.300x200.jpg (second thumbnail) With the default storage backend if photo.jpg already exists it will use these filenames: photo_.jpg photo_.125x125.jpg photo_.300x200.jpg **Note:** It assumes that if filename "any_filename.jpg" is available filenames with this format "any_filename.[widht]x[height].jpg" will be available, too.

  • image
  • models
  • fields
  • thumbnail
  • field
  • thumbnails
  • thumbs
Read More

SuperChoices

Seeing [snippet 1178](http://www.djangosnippets.org/snippets/1178/) reminded me that I also had a go at writing a Choices class at some point. I'm content with the result, but I doubt xgettext will discover your translation strings, which will no doubt be inconvenient. Here it is anyway, in all its overly-complicated glory :-) The following demo was pulled from the function's docstring tests. >>> simple = Choices("one", "two", "three") >>> simple Choices(one=0, two=1, three=2) >>> tuple(simple) ((0, u'ein'), (1, u'zwei'), (2, u'drei')) >>> (0, _('one')) in simple True >>> simple.ONE 0 >>> hasattr(simple, 'FOUR') False Ordering just follows the order that positional arguments were given. Keyword arguments are ordered by their value at appear after positional arguments. >>> [ key for key, val in simple ] [0, 1, 2] >>> Choices(one=1, two=2, three=3) Choices(one=1, two=2, three=3) A Mix of keyword and non-keyword arguments >>> Choices("one", two=2, three=3) Choices(one=0, two=2, three=3) Automatically generated values (for "one" below) should not clash. >>> Choices("one", none=0, three=1, four=2) Choices(one=3, none=0, three=1, four=2) Here is an example of combined usage, using different object types. >>> combined = Choices(one=1, two="two", three=None, four=False) >>> len(combined) 4 >>> (1, _('one')) in combined True >>> ('two', _('two')) in combined True >>> (None, _('three')) in combined True >>> (False, _('four')) in combined True And here is an empty choices set. Not sure why you would want this.... >>> empty = Choices() >>> empty Choices()

  • models
  • choices
Read More

Choices class

Yet another class to simplify field choices creation. Keeps order, allows i18n. Before: ONLINE = 0 OFFLINE = 1 STATES = ( (ONLINE, _('online')), (OFFLINE, _('offline')) ) state = models.IntegerField(choices=STATES, default=OFFLINE) After: STATES = Choices( ('ONLINE', _('online')), ('OFFLINE', _('offline')) ) state = models.IntegerField(choices=STATES, default=STATES.OFFLINE)

  • models
  • choices
Read More
Author: dc
  • 7
  • 8

Model dependency graph using graphviz (script)

Instructions: Set your environment variables, install graphviz, and run. Finds all models in your installed apps and makes a handsome graph of your their dependencies based on foreignkey relationships. Django models have green outlines, yours have purple. The edge styling could be changed based on edge type.

  • models
  • graph
  • graphviz
Read More

models with order (+admin editing)

Use this abstract model if you want to add "order" to a given model. Once you do, you will get automatic "up" and "down" links for each model row. One problem is that if the user sorts by another row, the up and down links are still there, but now don't make a lot of sense.

  • models
  • admin
  • order
Read More

93 snippets posted so far.