Created
August 5, 2011 06:47
-
-
Save stefanfoulis/1127039 to your computer and use it in GitHub Desktop.
Base64 Encoded Field with south support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
# Work in progress. The South part does not work correctly yet! | |
import base64 | |
from django.db import models | |
# Stolen from http://djangosnippets.org/snippets/1669/ | |
class Base64Field(models.TextField): | |
def contribute_to_class(self, cls, name): | |
if self.db_column is None: | |
self.db_column = name | |
self.field_name = name + '_base64' | |
super(Base64Field, self).contribute_to_class(cls, self.field_name) | |
setattr(cls, name, property(self.get_data, self.set_data)) | |
def get_data(self, obj): | |
return base64.decodestring(getattr(obj, self.field_name)) | |
def set_data(self, obj, data): | |
setattr(obj, self.field_name, base64.encodestring(data)) | |
try: | |
from south.modelsinspector import add_introspection_rules | |
add_introspection_rules( | |
[([Base64Field],[],{})], | |
["^database_email_backend\.fields\.Base64Field"] | |
) | |
except ImportError: | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# model field: | |
content = Base64Field(null=True, blank=True, default=None) | |
# frozen south data on first migration (creating the table with the base63 field) | |
{'content_base64': ('database_email_backend.fields.Base64Field', [], {'default': 'None', 'null': 'True', 'db_column': "'content'", 'blank': 'True'}),} | |
# running the first migration works fine, but adding another migration fails with | |
# ./bin/django schemamigration database_email_backend --auto | |
Traceback (most recent call last): | |
File "./bin/django", line 27, in <module> | |
djangoprojectrecipe.manage.main('project.settings.base') | |
File "/Users/stefanfoulis/.buildout/eggs/djangoprojectrecipe-1.1.1-py2.7.egg/djangoprojectrecipe/manage.py", line 18, in main | |
utility.execute() | |
File "/Users/stefanfoulis/.buildout/eggs/Django-1.3-py2.7.egg/django/core/management/__init__.py", line 379, in execute | |
self.fetch_command(subcommand).run_from_argv(self.argv) | |
File "/Users/stefanfoulis/.buildout/eggs/Django-1.3-py2.7.egg/django/core/management/base.py", line 191, in run_from_argv | |
self.execute(*args, **options.__dict__) | |
File "/Users/stefanfoulis/.buildout/eggs/Django-1.3-py2.7.egg/django/core/management/base.py", line 220, in execute | |
output = self.handle(*args, **options) | |
File "/Users/stefanfoulis/.buildout/eggs/South-0.7.3-py2.7.egg/south/management/commands/schemamigration.py", line 123, in handle | |
name = change_source.suggest_name() | |
File "/Users/stefanfoulis/.buildout/eggs/South-0.7.3-py2.7.egg/south/creator/changes.py", line 73, in suggest_name | |
for change_name, params in self.get_changes(): | |
File "/Users/stefanfoulis/.buildout/eggs/South-0.7.3-py2.7.egg/south/creator/changes.py", line 178, in get_changes | |
old_fields, old_meta, old_m2ms = self.split_model_def(self.old_orm[key], self.old_defs[key]) | |
File "/Users/stefanfoulis/.buildout/eggs/South-0.7.3-py2.7.egg/south/creator/changes.py", line 31, in split_model_def | |
elif isinstance(model._meta.get_field_by_name(name)[0], models.ManyToManyField): | |
File "/Users/stefanfoulis/.buildout/eggs/Django-1.3-py2.7.egg/django/db/models/options.py", line 311, in get_field_by_name | |
% (self.object_name, name)) | |
django.db.models.fields.FieldDoesNotExist: Attachment has no field named 'content_base64' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment