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

IndexError: list index out of range #252

Open
ammsa opened this issue Nov 8, 2016 · 1 comment
Open

IndexError: list index out of range #252

ammsa opened this issue Nov 8, 2016 · 1 comment

Comments

@ammsa
Copy link

ammsa commented Nov 8, 2016

I have this view:

@method_decorator(login_required, name='dispatch')
class EventCreateView(generic_view.CreateView):
    model = Event
    template_name = 'event/create.html'
    form_class = EventForm
    object = None

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.save()
        return HttpResponseRedirect(self.get_success_url())

and the following models


class Event(AbstractDateInfo, PolymorphicModel):

    user = models.ForeignKey(User)
    name = models.CharField(max_length=50,
                            blank=False,
                            null=False)
   
class OneToOneEvent(Event):
   ...


class GroupEvent(Event):
   ....

For some reason I was unable to create an Event object. Here is the trace

▶ event/views.py in form_valid
    form_class = EventForm
    object = None
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.save() ...
        return HttpResponseRedirect(self.get_success_url())
▶ lib/python2.7/site-packages/polymorphic/models.py in save
            self.polymorphic_ctype = ContentType.objects.db_manager(using).get_for_model(self, for_concrete_model=False)
    pre_save_polymorphic.alters_data = True
    def save(self, *args, **kwargs):
        """Overridden model save function which supports the polymorphism
        functionality (through pre_save_polymorphic)."""
        using = kwargs.get('using', self._state.db or DEFAULT_DB_ALIAS)
        self.pre_save_polymorphic(using=using) ...
        return super(PolymorphicModel, self).save(*args, **kwargs)
    save.alters_data = True
    def get_real_instance_class(self):
        """
        Normally not needed.
▶ lib/python2.7/site-packages/polymorphic/models.py in pre_save_polymorphic
        """Normally not needed.
        This function may be called manually in special use-cases. When the object
        is saved for the first time, we store its real class in polymorphic_ctype.
        When the object later is retrieved by PolymorphicQuerySet, it uses this
        field to figure out the real class of this object
        (used by PolymorphicQuerySet._get_real_instances)
        """
        if not self.polymorphic_ctype_id: ...
            self.polymorphic_ctype = ContentType.objects.db_manager(using).get_for_model(self, for_concrete_model=False)
    pre_save_polymorphic.alters_data = True
    def save(self, *args, **kwargs):
        """Overridden model save function which supports the polymorphism
        functionality (through pre_save_polymorphic)."""
▶ lib/python2.7/site-packages/django/db/models/query_utils.py in __get__
        data = instance.__dict__
        if data.get(self.field_name, self) is self:
            # self.field_name is the attname of the field, but only() takes the
            # actual name, so we need to translate it here.
            try:
                f = opts.get_field(self.field_name)
            except FieldDoesNotExist:
                f = [f for f in opts.fields if f.attname == self.field_name][0] ...
            name = f.name
            # Let's see if the field is part of the parent chain. If so we
            # might be able to reuse the already loaded value. Refs #18343.
            val = self._check_parent_chain(instance, name)
            if val is None:
                instance.refresh_from_db(fields=[self.field_name])

Error occurred here:

            f = [f for f in opts.fields if f.attname == self.field_name][0] ...

The cause is probably because I'm inheriting a abstract class AbstractDateInfo, as well as PolymorphicModel. Not sure if it's something allowed to be done using django-polymorphic

@vdboor
Copy link
Collaborator

vdboor commented Nov 10, 2016

You can try switching the inheritance order, this has helped me fixing another project.

This is something we'd like to fix, but causes rather complex issues within the ORM.

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

2 participants