如何将其他数据传递给post_ajax方法?

时间:2019-02-07 08:08:15

标签: python ajax django django-views dropzone.js

我有以下型号:

class MyModelOne(models.Model):
    title = models.CharField(verbose_name=_('Title'), max_length=255)
    body = models.TextField(verbose_name=_('Body'))
    # some other fields


class MyModelTwo(models.Model)
    title = models.CharField(verbose_name=_('Title'), max_length=255)
    # some other fields


class Photo(models.Model):
    title = models.CharField(verbose_name=_('Title'), max_length=255)
    image = StdImageField(verbose_name=_('Image'),upload_to='photos')
    content_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

我想包含一个dropzone(DropzoneJS)字段,用于在更新其他对象的同时在其他一些模型的管理员更改视图中创建Photos对象。我正在使用ajax视图来独立于管理员更改视图表单来处理它。

要创建Photo对象,我需要将当前更新的其他模型的数据传递给post_ajax方法。

我不知道如何在我想处理创建照片的post_ajax方法中识别当前对象模型及其ID。

from braces.views import AjaxResponseMixin, JSONResponseMixin, LoginRequiredMixin, SuperuserRequiredMixin

class AjaxPhotoUploadView(
    LoginRequiredMixin,
    SuperuserRequiredMixin,
    JSONResponseMixin,
    AjaxResponseMixin,
    View,
):
    def post_ajax(self, request, *args, **kwargs):
        file = request.FILES['file']
        obj = ??? # I need to get the object somewhere here but I do not know how
        Photo.objects.create(
            title=file.name,
            image=file,
            content_object=obj, 
        )
        response_dict = {
            'message': 'File uploaded successfully!',
        }
        return self.render_json_response(response_dict, status=200)

0 个答案:

没有答案