2π
In the end this solution worked for me, credits to Shai over at googlegroups:
South, when it tries to build an instance of your custom field, collects (using
the rules) values for the different kwargs of your class. One of these is the
"to"
argument; you see it specified in the migration code.
Then, it takes the values it collected and passes them to a constructor. So,
effectively, you get the call
fields.ParentField(related_name='set', to=orm['module.ModelClass'])
However, inside your __init__
this gets translated to
models.ForeignKey('self', related_name='set', to=orm['module.ModelClass'])
So there are two values for the 'to'
argument β the first is the positional
'self'
.
One solution, at least, is to do the same thing I recommended for the null and
blank arguments β add it into kwargs, rather than directly to the call.
kwargs.setdefault('to', 'self')
0π
I had similar issue but solved it by calling: kwargs.pop('to', None)
.
However, there was next error:
File "/.../django/db/models/fields/related.py", line 973, in resolve_related_fields
to_field = (self.rel.to._meta.pk if to_field_name is None
AttributeError: 'str' object has no attribute '_meta'
This was because I was passing model name as a string not class instance. South doesnβt support that. Fixed by importing model and using it instead of string.
Full working example below:
from django.db import models
from django.conf import settings
from django.db.models.loading import get_model
def _get_user_model():
"""Import django.contrib.auth.models.User or other model set in settings.AUTH_USER_MODEL"""
model_name = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
return get_model(*model_name.split('.', 1))
def _set_default_kwargs(kwargs):
kwargs.setdefault('on_delete', models.SET_NULL)
kwargs.setdefault('blank', True)
kwargs.setdefault('null', True)
kwargs.pop('to', None) # Remove kwargs['to'] parameter set by south migrations
return kwargs
class ForeignKeyWithUser(models.ForeignKey):
def __init__(self, **kwargs):
kwargs = _set_default_kwargs(kwargs)
User = _get_user_model()
return super(ForeignKeyWithUser, self).__init__(User, **kwargs)
try:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^toolkit\.models\.fields\.ForeignKeyWithUser"])
except ImportError:
pass
- [Answered ]-Start over with django migrations
- [Answered ]-Error in creating superuser django
- [Answered ]-404 Error "No Blog entry found matching the query" Django admin issue
- [Answered ]-Django object filtering with variables
- [Answered ]-Django "Instance" Form Fields Not Rendering