2

Problem

I need to display a gallery of images on a product page. This worked when we were at Django 1.6, but have since upgraded to Django 1.11 (Big Process). I am now stuck at how to get this to work within the new environment. Right now clicking Add Image brings up a pop up where I can select the image, and the regions associated with it (US, Canada, Spain, Etc..), but after clicking "Save" The popup title changes to Popup closing... and never closes - also the image is not added to the gallery. The image I upload itself IS added to the rest of the Images within filer, however it is not added to the ProductGallery Model.

What I've Got

Django: 1.11.7

Django-Filer: 1.2.7

Django-Suit: 0.2.25

Vanilla-Views: 1.0.4

I have Product models, these products have a many to many relationship to a ProductGallery model like so:

class Product(models.Model):
    gallery = models.ManyToManyField('products.ProductGallery')

The ProductGallery is supposed to house Images and Videos allowing for upload of either, and providing one list to iterate through on the front end for display purposes.

The ProductGallery is defined as:

class ProductGallery(models.Model):
    limit = models.Q(app_label='media', model='image') | models.Q(app_label='media', model='video')

    order = models.PositiveIntegerField(default=0)
    content_type = models.ForeignKey(ContentType, limit_choices_to=limit)
    object_id = models.PositiveIntegerField(db_index=True)
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    class Meta:
        ordering = ('order',)

    def __str__(self):
        return six.text_type(self.content_object)

where media.image is defined as: (I'll ignore video for the time being)

class Image(CountryInfoModel, models.Model):
    image = FilerImageField(null=True, blank=True)

    def __str__(self):
        return str(self.image.name or self.image.original_filename)

I've got a view for Adding new Media like so:

class AddMedia(LoginRequiredMixin, StaffuserRequiredMixin, JsonRequestResponseMixin, GenericView):
    require_json = True

    def post(self, request, *args, **kwargs):
        object_id = self.request_json["objectId"]
        object_var = self.request_json["objectVarName"]
        content_type_id = self.request_json["contentType"]
        order = self.request_json["order"]
        media_id = self.request_json["mediaId"]
        media_type = self.request_json["mediaType"]

        content_type = _get_content_type_or_404(content_type_id)
        content_object = _get_object_or_404(content_type, object_id)
        model_var = getattr(content_object, object_var)

        try:
            if media_type.lower() == "image":
                obj = Image.objects.get(pk=media_id)
            elif media_type.lower() == "video":
                obj = Video.objects.get(pk=media_id)
            else:
                raise Http404("Invalid mediaType parameter: {0}".format(media_type))
            media_item = model_var.create(content_object=obj)
            media_item.order = order
            media_item.save()
        except model_var.model.DoesNotExist:
            pass

        return self.render_json_response({'message': "Order successfully updated"})

And I think thats all the pieces there are to this. I am lost on why when I click "save" the Image is not saved to the ProductGallery model at all. I'd be happy to provide more context if needed, and any help is very much appreciated.

BMoe872
  • 141
  • 2
  • 12

1 Answers1

0

Just in case anyone else comes across this and wants to know how it was fixed.

It turns out that some of the django-admin template functionality had been overwritten and was causing some issues.

Specifically this project had overwritten parts of the save button functionality. The function dismissAddRelatedObjectPopup used to be named dismissAddAnotherPopup

These functions were overwritten to provide the custom functionality outlined above with the ProductGallery. Django went to call the function, but this was throwing an error on the popup related to something called SelectBoxwhich then broke the ajax call that was needed to save the model correctly.

Hopefully this can help someone in the future!

BMoe872
  • 141
  • 2
  • 12