Django South: How to create rules for a Custom Field?
I've create a custom field "Private FileFiel开发者_如何学编程d". I'm not able to get it to work with django-south.
My understanding of South field rules is based on http://south.aeracode.org/docs/tutorial/part4.html#tutorial-part-4 and http://south.aeracode.org/docs/customfields.html
Relevant snippets are:
class FileField(models.CharField):
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
if not 'upload_to' in kwargs:
raise Exception("%s expects one keyword arg 'upload_to'" % (self.__class__))
self.upload_to = kwargs['upload_to']
del kwargs['upload_to']
kwargs['max_length'] = 255
super(FileField, self).__init__(*args, **kwargs)
and
rules = [
(
(FileField,),
[],
{
"upload_to": ["upload_to", {}],
},
)
]
from south.modelsinspector import add_introspection_rules
add_introspection_rules(rules, ["^private_filefield\."])
Running a manage.py schemamigration my_app_name --auto fails with the following message:
Exception: <class 'private_filefield.fields.FileField'> expects one keyword arg 'upload_to'
(this happes when site-packages/south/orm.py", line 46, in FakeORM is called)
Full code can be found on: http://bitbucket.org/vanschelven/django_private_filefield/src/tip/private_filefield/fields.py
=== Edit: text below added ===
This is the relevant section of the generated 'models' section of the auto-generated migration:
'mailfile.mailfile': {
'Meta': {'object_name': 'MailFile'},
'creation_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'expires_on': ('django.db.models.fields.DateField', [], {'default': 'datetime.date(2010, 7, 16)'}),
'file': ('private_filefield.fields.FileField', [], {'max_length': '255'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
'secret': ('django.db.models.fields.CharField', [], {'max_length': '40'})
}
Note the lack of 'upload_to' as paramter to 'file'.
Bah, I tried to write this in a comment, but the lack of paragraphs hates me.
I just set up your custom field as-is in a Django app, and created a dummy model to use it. South works perfectly. I added another FileField and was able to run schemamigration --auto
with no issues. So, I'm pretty sure you have South set up correctly.
Honestly, did you check your model to make sure that it has the upload_to
parameter? That causes exactly this error message (and it would mean South did exactly what you told it to do).
精彩评论