11

I get this exception:

django.core.exceptions.FieldError:

Local field 'ticket' in class 'SpecialPlugin' clashes with field of similar name from base class 'BasePlugin'

Here are my models:

class BasePlugin(models.Model):
    ticket = models.OneToOneField('foobar.ticket', primary_key=True, 
                                  related_name='%(app_label)s_%(class)s')

    class Meta(IndexImplementation.Meta):
        abstract = True

    # .. Other stuff which should be available for SpecialPlugin 
    #    and other child classes.

class SpecialPlugin(BasePlugin):
    ticket = models.OneToOneField('foobar.ticket', primary_key=True, 
                                  related_name='special')

I only found this note, but in my case the parent class is abstract. I am unsure if it applies here.

I want to give the child class SpecialPlugin the related name "special" since the related name (%(app_label)s_%(class)s) of the BasePlugin would break old code.

Is there a way to give SpecialPlugin.ticket the related_name "special"?

2
  • This error should only arise if the parent class is not abstract. I've just tested with simple models analogous to yours, and it works fine. If you look in the Django code that raises the exception, it checks whether the parent class is abstract or not. This suggests to me that BasePlugin isn't correctly being set as abstract. What is in IndexImplementation.Meta? Commented Mar 26, 2017 at 4:16
  • @solarissmoke IndexImplementation.Meta is abstract = True. That's why I don't understand the error message.
    – guettli
    Commented Mar 27, 2017 at 15:09

3 Answers 3

15
+50

Update:

Similar thing can be done using django's default_related_name meta option.


It might look like an ugly hack, but you can set a function call to the related_name argument instead of string. And then override that function in the child class/model.

class BasePlugin(models.Model):

    @staticmethod
    def get_ticket_related_name():
        return '%(app_label)s_%(class)s'

    ticket = models.OneToOneField('foobar.ticket', primary_key=True, 
                                  related_name=get_ticket_related_name.__func__())

    class Meta(IndexImplementation.Meta):
        abstract = True


class SpecialPlugin(BasePlugin):
    @staticmethod
    def get_ticket_related_name():
        return 'special'
6
  • 3
    The get_ticket_related_name in my subclasses is never called.
    – kzh
    Commented Sep 1, 2017 at 14:33
  • @kzh Did you make sure: 1. Your base class is abstract. 2. Changing sequence of appearance/declaration on get_ticket_related_name method in subclass fixes it?
    – v1k45
    Commented Sep 1, 2017 at 15:29
  • I'll try to arrange sequence. Thanks.
    – kzh
    Commented Sep 1, 2017 at 16:00
  • @v1k45 I just tried this solution, but it doesn't seem to be taking. The subclass defines the static method 'get_ticket_related_name'. But the baseclass's method is the one being called. (And the baseclass is abstract) The only difference I can see is the Meta of the Base is using IndexImplementation, where mine is not. But I can't seem to find what that class is?
    – albrnick
    Commented Mar 26, 2019 at 16:53
  • 2
    @albrnick I'm not sure about this, but I have discovered that something similar already exists in django. Lookup default_related_name meta option. docs.djangoproject.com/en/2.0/ref/models/options/…
    – v1k45
    Commented Mar 27, 2019 at 19:35
2

Using default related name in Meta class:

class SpecialPlugin(BasePlugin):
    class Meta:
        default_related_name = 'special_plugins'
0

It looks like the core of the problem is in the overriding of model field Django model inheritance, overriding fields

Simple workaround for you problem will be to decouple BasePlugin to to class without ticket field and then create a child class that contains ticket field

class BaseWithoutTicketPlugin(models.Model):
    # .. Other stuff which should be available for SpecialPlugin 
    #    and other child classes.
    class Meta(IndexImplementation.Meta):
        abstract = True

class BasePlugin(BaseWithoutTicketPlugin):
    ticket = models.OneToOneField('foobar.ticket', primary_key=True, 
                                  related_name='%(app_label)s_%(class)s')

    class Meta(BaseWithoutTicketPlugin.Meta):
        abstract = True


class SpecialPlugin(BaseWithoutTicketPlugin):
    ticket = models.OneToOneField('foobar.ticket', primary_key=True, 
                                  related_name='special')

Idea is to use BaseWithoutTicketPlugin when you need to customize ticket and use BasePlugin when you don't.

3
  • BasePlugin has other attributes which are needed. I need to inherit from it. Sorry, I was stripping down my code for the question ... maybe to much was stripped. I update the question.
    – guettli
    Commented Mar 20, 2017 at 10:25
  • @guettli Still don't see problem in my suggestion, I updated my answer. Commented Mar 20, 2017 at 10:31
  • the "Other stuff" part needs the attribute "ticket". This means you need to move this part to "BasePlugin". After doing this, the other-stuff-part is no more available for SpecialPlugin :-(
    – guettli
    Commented Mar 20, 2017 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.