Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The objects which were transmogrified aren't initialized correctly if they implement __init__ method. #615

Open
bdabrowski opened this issue Aug 19, 2024 · 1 comment

Comments

@bdabrowski
Copy link

When the real_object has a different class from real_class, it gets transmogrified into real_class.

When it implements __init__, it is not called.

#614

@bdabrowski
Copy link
Author

bdabrowski commented Aug 20, 2024

In the current situation, we experience demonstrated issue:

# models for https://github.com/jazzband/django-polymorphic/issues/615

class Duck(PolymorphicModel):
    name = models.CharField(max_length=30)
    

class BlueHeadDuck(Duck):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.color = "blue"
        

class HomeDuck(models.Model):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.home = "Duckburg"
        
    class Meta:
        abstract = True


class PurpleHeadDuck(HomeDuck, BlueHeadDuck):
    class Meta:
        proxy = True

Test

    def test_transmogrify_with_init(db):
        pur = PurpleHeadDuck.objects.create()
        assert pur.color == "blue"
        assert pur.home == "Duckburg"
    
        pur = Duck.objects.get(id=pur.id)
        assert pur.color == "blue"
        # issues/615 fixes following line:
>       assert pur.home == "Duckburg"
E       AttributeError: 'PurpleHeadDuck' object has no attribute 'home'
    

The code to handle __init__ was there, but it wasn't run due to a condition error.

The code was there, so I assume it was intended for cases such as this one. But I haven't seen this condition fulfilled in any other existing unit test.

I know that Django triggers __init__ only on concrete models but the existence of __init__ with super makes sure it is called for each eventually. I didn't observe this for polymorphic and hope to fix it with my change.

bdabrowski added a commit to bdabrowski/django-polymorphic that referenced this issue Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant