Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class function is not found

I have the problem with creation of Referral from pinax-referrals package. Referral class has class function create(...) When I am trying to create referral inside view like:

from pinax.referrals.models import Referral

def createReferral(user):

    referral = Referral.create(
        user = user,
        redirect_to = "/"
    )

It throws me following error:

type object 'Referral' has no attribute 'create'

The code inside Pinax model looks ok:

@classmethod
def create(cls, redirect_to, user=None, label="", target=None):
    if target:
        obj, _ = cls.objects.get_or_create(
            user=user,
            redirect_to=redirect_to,
            label=label,
            target_content_type=ContentType.objects.get_for_model(target),
            target_object_id=target.pk
        )
    else:
        obj, _ = cls.objects.get_or_create(
            user=user,
            label=label,
            redirect_to=redirect_to,
        )

    return obj

As I understand the problem is not connected to the Pinax package itself and looks really strange. Does anybody have any ideas?

like image 689
Alexander Tyapkov Avatar asked Mar 03 '26 18:03

Alexander Tyapkov


1 Answers

It sounds like you have defined another class Referral inside the same module, that has replaced Pinax's Referral model.

This could happen because you have defined a class,

class Referral(View):
    ...

or maybe you have imported another class Referral. It might not be obvious this has happened if you do a * import.

from mymodule import *

A useful tool to debug is to add print(Referral) to you view. Then you will see whether the Referral class is the one you expect.

like image 137
Alasdair Avatar answered Mar 06 '26 07:03

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!