Skip to main content

Posts

Normalize '__unicode__' accross django models

Don't repeat your self, use monkeypatching - a better default '__unicode__' This latest top-secret project for sure is keeping me busy and time wise leaves little to keep blogging, however it being Friday and all and because you're worth it here is a Django expert tip. Django's default '__unicode__' representation leaves a bit to be desired to say the least. In a nut shell it returns the class name + 'object' for all instances and so requires that all your models have a '__unicode__' defined to have a useful string represenation. Well with a bit of friendly monkeys we can patch it right up as per the code below: from django.db.models.base import Model import logging import hashlib import re from django.conf import settings logger = logging.getLogger(__name__) #---------------------------------------------------------------------------------------------------------------------- logger.info("Patching 'django.db.models.base.M...

Django Custom Chainable QuerySets

How to reuse custom defined 'models.Manager' QuerySets Django recommends to define custom query sets on your sublcassed models.Manager, there is one problem however that these are not chainable, that is you can only call your custom queryset once - assuming I have defined an '.active()' queryset the following will not work: >> PaymentTerm.objects.active().active() Traceback (most recent call last): File " ", line 1, in AttributeError: 'PaymentTermManager' object has no attribute 'active' >>> Current solutions on the web involve creating a subclassed 'models.Manager' and a custom 'QuerySet' which do work but are to code verbose for my linking. I believe the below approach is simpler, cleaner and more succinct. class OfferManager(models.Manager): ... STATUS_CHOICES = ( (STATUS_DISABLED, "Disabled"), (STATUS_ENABLED, "Enabled"), (STATUS_NEGOTIATED, "Negotiated")...

CommandError: Unable to serialize database: Error encountered checking Geometry returned from GEOS C function "GEOSWKBReader_read_r".

ERROR django.contrib.gis libgeos.py@80: GEOS_ERROR: ParseException: Unknown WKB type 0 These cryptic messages are caused by using the vanilla Django `model.Manager` with your geo enabled models. This caused me a frew hours of grief trying to figure it out, thinking it's the import data, or an issue with the third party app, etc. but the fix is to just extend your custom model manager from the `gis.models.GeoManager` which is super easy to forget: from django.contrib.gis.db import models class MyCustomManager(models.GeoManager): """ Additional methods / constants to Base's objects manager - using a GeoManager is fine even for plain models: ``BaseManager.objects.public()`` - all instances that are asccessible through front end """ # Model (db table) wide constants - we put these and not in model definition to avoid circular imports. # one can access these constants through .objects.STATUS_DISABLED or ImageManager.S...

Django import fixtures IntegrityError: Problem installing fixtures:

IntegrityError: Problem installing fixtures: The row in table 'geonames_alternatename' with primary key '1830' has an invalid foreign key: geonames_alternatename.locality_id contains a value '109436' that does not have a corresponding value in geonames_locality.geonameid. In a situation where a third party app data export works just fine but the fixutre fails due to any IntegrityError is most likley cause by an overiden `objects` manager that returns only a subset. The solution is to explicitly tell django to export using the default model managers using the `--all` argument for example like so: dumpdata geonames --indent=4 --all --settings=settings_development > geonames_ext/fixtures/initial_data.json This is clearly a situation where explicit is better then implicit, that is when defining custom model managers do not touch the `get_query_set` method rather instead add an explict method; for post completeness below is my Eclipse PyDev template I utli...

Unable to configure handler 'console': __init__() got an unexpected keyword argument 'strm'

Django and upgraded from 2.6 to Python 2.7 Hopefuly this quick post will save someone time, I recently upgraded our Debian servers from stable (squeeze) to testing (wheezy). Unfortunately this resulted in an odd error: Traceback (most recent call last): File "manage.py", line 12, in execute_from_command_line(sys.argv) File "/srv/www/django/development.FOO.com/virtualenv/src/github-django-django-stable/django/core/management/__init__.py", line 453, in execute_from_command_line utility.execute() File "/srv/www/django/development.FOO.com/virtualenv/src/github-django-django-stable/django/core/management/__init__.py", line 392, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/srv/www/django/development.FOO.com/virtualenv/src/github-django-django-stable/django/core/management/__init__.py", line 263, in fetch_command app_name = get_commands()[subcommand] File "/srv/www/django/development.F...

mppe_compress[0]: osize too small! (have: 1404 need: 1408)

Windows VPN and PPTP Connectivity Timeouts In my pursuit for a personal cloud and convenient secure remote file system so as to be able to develop directly on the server I have researched, tested and decided against SFTP, WebDAV, and SAMBA/NFS over internet. The only approach still standing is the built in Windows VPN and PPTP + SAMBA/CIFS: However there was one bug that prevented some sites/services from working and timing out; the /var/log/syslog showed the following intermittently: Apr 19 10:15:15 sh1 pptpd[11330]: GRE: accepting packet #107 Apr 19 10:15:15 sh1 pptpd[11330]: GRE: accepting packet #108 Apr 19 10:15:15 sh1 pptpd[11330]: GRE: accepting packet #109 Apr 19 10:15:15 sh1 kernel: mppe_compress[0]: osize too small! (have: 1404 need: 1408) Apr 19 10:15:15 sh1 kernel: ppp0: ppp: compressor dropped pkt Apr 19 10:15:15 sh1 kernel: mppe_compress[0]: osize too small! (have: 1404 need: 1408) This due to the way  MPPE Microsoft point-to-point Encryption encodes data ...

Another Awesome Free Linode VPS Upgrade

Double memory, double CPU and 10x internet traffic. Our hosting of choice for self managed servers is Linode as this company continuously stays competitive and keeps giving back to it's customers. In the last two months they doubled the memory, updated CPU cores, increased transfer cap, and storage space you can read their blog for more info. Plan RAM Disk XFER CPU Price Linode 1G 1 GB 24 GB 2 TB 8 cores (1x priority) $20 / mo Linode 2G 2 GB 48 GB 4 TB 8 cores (2x priority) $40 / mo Linode 4G 4 GB 96 GB 8 TB 8 cores (4x priority) $80 / mo Linode 8G 8 GB 192 GB 16 TB 8 cores (8x priority) $160 / mo Linode 16G 16 GB 384 GB 20 TB 8 cores (16x priority) $320 / mo Linode 24G 24 GB 576 GB 20 TB 8 cores (24x priority) $480 / mo Linode 32G 32 GB 768 GB 20 TB 8 cores (32x priority) $640 / mo Linode 40G 40 GB 960 GB 20 TB 8 cores (40x priority) $800 / mo In my opinion the best VPS for a low cost $20 a month pla...