Skip to content

IndexError: list index out of range #252

@ammsa

Description

@ammsa

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions