Login

Tag "django1.1"

Snippet List

Export Django data to datestamped tarball -- choose individual models for handy packaging and archiving

Just like it says -- set it up and run. Use it for server migrations, for project handoffs, in cron jobs, you name it. I have never had problems exporting models to individual fixtures in this way, and only one bout of trouble re-importing them (and that was, like, an impossible-triangle of OneToOneField dependencies anyway, and they were going from a sqlite file to a postgres schema that totally had inappropriate nullable columns in it). I find that the json files named for what they contain is helpful when and if manage.py does freak out during an import, as the output from `loaddata` command is so opaque it's autistic, basically. A trivial refactoring effort could make it into a management command -- it already makes use of the builtin `dumpdata` command class internally. However I did not feel like overthinking it enough to set it up in a repository (doubtlessly padded with unrelated 'utilities' and explanatory .rst files) and then writing a blog post to sell it to you. That is why you are reading this code here, instead of on GitHub. Don't get me wrong, GitHub is awesome, and like a consummate host... but not the way I love me some quick and dirty snippet code, these days. Whatever, you say lazy, I say productively relaxed, potato/potahto. Erm. In any case please do enjoy this model fixture-exporter. Yes.

  • django
  • python
  • json
  • export
  • data
  • script
  • command
  • archive
  • django1.1
  • backup
  • datestamp
  • tar
  • tarball
Read More

TaggedManager and TaggedQuerySet with chainable tagged() methods implemented with django-tagging

The popular [django-tagging](http://code.google.com/p/django-tagging/) app has, in its implementation and semantics, a highly usable and transparent elegance -- but then you have to call methods on a Tag instances' items collection. These classes let you inline the tag name in the chain of queryset filter methods instead. TO USE: ### models.py ... from tagging.fields import TagField from tagging.models import Tag as Tag class YourModel(models.Model): ... yourtags = TagField() objects = TaggedManager() ... ### and then elsewhere, something like-- ... ym = YourModel.objects.order_by("-modifydate")[0] anotherym = YourModel.objects.get(id=7) ## distinct from ym ym.yourtags = "tag1 tag2" anotherym.yourtags = "tag1 othertag" ym.save() anotherym.save() with_tag1 = YourModel.objects.tagged('tag1') with_tag2 = YourModel.objects.tagged('tag2').order_by('-modifydate') print ym in with_tag1 ## True print anotherym in with_tag1 ## True print ym in with_tag2 ## False ... since these are QuerySets, you can easily create unions (e.g. `with_tag1 | with_tag2` and othersuch) as you need and filter them to your hearts' content, without having to instantiate Tag all the time (which you can of course do as well).

  • manager
  • queryset
  • django-tagging
  • django1.1
  • chainable
Read More

2 snippets posted so far.