Issue #1 resolved

Using 0.2.4 results in a `related_name` error.

Thomas Schreiber
created an issue

It has to do with not setting the related_name field on the ForeignKey subclass. {{{

!python

File "/home/rizumu/.virtualenvs/snowprayers/lib/python2.7/site-packages/licenses/fields.py", line 17, in init super(ForeignKey, self).init('licenses.License', args, *kwargs) TypeError: init() got multiple values for keyword argument 'verbose_name'

}}}

I've dug a little deeper, taking example from django/db/models/fields/related.py ForeignKey and found that something like this works:

{{{

!diff

diff -r fa9fc69d0314 licenses/fields.py --- a/licenses/fields.py Fri May 27 09:49:19 2011 +0200 +++ b/licenses/fields.py Sun Nov 13 11:57:55 2011 +0100 @@ -1,6 +1,8 @@ # -- coding: utf-8 -- -from django.db.models import ForeignKey +from django.db.models import ForeignKey, ManyToOneRel from django.utils.translation import ugettext_lazy as _ +from django.db.models.deletion import CASCADE +from licenses.models import Organization

class LicenseField(ForeignKey): @@ -9,12 +11,21 @@ and a shortcut for switching blank/null to True. '''

  • def init(self, args, *kwargs):
  • def init(self, to=Organization, to_field=None, rel_class=ManyToOneRel, **kwargs): kwargs.setdefault('verbose_name', _('license')) if kwargs.pop('required', None) == False: kwargs['blank'] = True kwargs['null'] = True
  • super(ForeignKey, self).init('licenses.License', args, *kwargs)
  • if 'db_index' not in kwargs:
  • kwargs['db_index'] = True +
  • kwargs['rel'] = rel_class(to, to_field,
  • related_name=kwargs.pop('related_name', None),
  • limit_choices_to=kwargs.pop('limit_choices_to', None),
  • parent_link=kwargs.pop('parent_link', False),
  • on_delete=kwargs.pop('on_delete', CASCADE),
  • )
  • super(ForeignKey, self).init(**kwargs)

}}}

Comments (2)

  1. Frank Becker

    Hi,

    I run into the same issue. However, the following diff works for me. I guess the 1st argument to super should be LicenseField, right?

    diff -r fa9fc69d0314 licenses/fields.py
    --- a/licenses/fields.py        Fri May 27 09:49:19 2011 +0200
    +++ b/licenses/fields.py        Wed May 09 00:29:16 2012 +0200
    @@ -1,6 +1,7 @@
     # -*- coding: utf-8 -*-
     from django.db.models import ForeignKey
     from django.utils.translation import ugettext_lazy as _
    +from licenses.models import Organization
     
     
     class LicenseField(ForeignKey):
    @@ -9,12 +10,12 @@
             and a shortcut for switching blank/null to True.
         '''
     
    -    def __init__(self, *args, **kwargs):
    +    def __init__(self, to=Organization, *args, **kwargs):
             kwargs.setdefault('verbose_name', _('license'))
             if kwargs.pop('required', None) == False:
                 kwargs['blank'] = True
                 kwargs['null'] = True
    -        super(ForeignKey, self).__init__('licenses.License', *args, **kwargs)
    +        super(LicenseField, self).__init__('licenses.License', *args, **kwargs)
     
     
     try:
    @@ -22,3 +23,4 @@
         add_introspection_rules([], ['^licenses\.fields\.LicenseField'])
     except ImportError:
         pass
    +
    

    Just for completeness, there seems to be an issue w/ South right now. One has to

    -        super(LicenseField, self).__init__('licenses.License', *args, **kwargs)
    +       super(LicenseField, self).__init__(License, *args, **kwargs)
    

    and also from licenses.models import License in order to work around that. Anyhow, that isn't a bug in django-licenses.

  2. Log in to comment