[Fixed]-Override Oscar models using other Oscar models from the same app

1👍

When overriding Oscar models you need to move any non-abstract imports below the class definitions that are meant to replace models that come with Oscar. Here’s an example.

In your case it should be safe to just move the models import below:

from oscar.apps.catalogue.abstract_models import AbstractProduct


Product(AbstractProduct):
    @property
    display(self):
        if self.product_class == ProductClass.objects.get(pk=1):
            #do something
        else:
            #do something else


from oscar.apps.catalogue.models import *

The display property will continue to work since by the time it is called for the first time, the module scope will already contain all the necessary models.

I’ve changed the import to * to make sure that all other models are loaded from Oscar.

Please note that the star import will inevitably try to import the Product model as well but Oscar will notice that a model by that name is already registered and will simply discard the other definition. It is ugly and confusing but that’s the Oscar’s recommended method.

👤patrys

Leave a comment