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

model fields/properties/methods are being clobbered #266

Open
smcoll opened this issue Feb 16, 2017 · 1 comment
Open

model fields/properties/methods are being clobbered #266

smcoll opened this issue Feb 16, 2017 · 1 comment

Comments

@smcoll
Copy link

smcoll commented Feb 16, 2017

Let's say we have the following models:

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

class Foo(Base):
    foo = models.CharField(max_length=30)

We can't set Foo.foo because Foo.foo is overwritten as an attribute referencing itself... (i believe that is happening here):

>>> Foo.objects.create(foo='some foo text')
ValueError: Cannot assign "'some foo text'": "Foo.foo" must be a "Foo" instance.

In fact, if model Bar is a subclass of Base and has a foo field/method/property, we have the same problem, because the foo attribute for every subclass of Base is trying to reference a Foo instance.

i'm not sure what the internal use of that property is, but how about we allow the name of the property to be overridden (via a method on the base class which could be overridden), or perhaps auto-generate a name like _foo instead? Otherwise, this means that no field on any child model can match the name of its own class or of a class having the same base.

@vdboor
Copy link
Collaborator

vdboor commented Apr 6, 2017

This issue is caused by Django's way of dealing with model inheritance (see: https://docs.djangoproject.com/en/dev/topics/db/models/#specifying-the-parent-link-field)
django-polymorphic "mainly" adds improved retrieval of these objects, and form/admin interfaces.

To have model inheritance, Django adds an implicit base_ptr = OneToOneField(Base, related_name='foo', primary_key=True, parent_link=True) field to Foo that acts as primary key to the Base model. When you explicitly define the key, you might be able to change the related_name so they don't clash with your properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants