AttributeError:'QuerySet'对象没有属性'对象'

时间:2016-08-26 10:19:22

标签: django django-rest-framework

尝试使用django rest框架上传图片,我只是不知道如何编写逻辑,,,这里是我的

  

models.py

...

class ProductsTbl(models.Model):
    ....
    model_number = models.CharField(
        max_length=255,
        unique=True,
        error_messages={
            'unique': "這 model number 已經被註冊了 ."
        }
    )
    slug = models.SlugField(unique=True)
    ....

class Upload(models.Model):
    thing = models.ForeignKey(ProductsTbl, related_name="uploads")
    ......
  

应用程序/ API / serializers.py

........
class UploadSerializers(serializers.ModelSerializer):

        image = serializers.ImageField(max_length=None, use_url=True)

        class Meta:
            model = Upload
            fields = (
                'id',
                'image',
                'thing',
                'description'
                )



class ProductsTblSerializer(serializers.ModelSerializer):
    uploads = UploadSerializers(many=True, read_only=True)
    ......
    class Meta:
        model = ProductsTbl
        fields = (
                .....
                'model_number',
                'slug',
                ........
                'uploads'
                 )

.....
  

应用程序/ API / urls.py

....

urlpatterns = [
    url(r'^productsTbls/$',
        views.ProductsTblListView.as_view(),
        name='productsTbls_list'),
    url(r'^productsTbls/(?P<pk>\d+)/$',
        views.ProductsTblDetailView.as_view(),
        name='productsTbls_detail'),

    url(r'^productsTbls/(?P<slug>[-\w]+)/uploads/$',
        views.UploadDetailView.as_view(),
        name='uploads'),

]
  

应用程序/ API / views.py

......

class ProductsTblListView(generics.ListCreateAPIView):
    queryset = ProductsTbl.objects.order_by('-created')
    serializer_class = ProductsTblSerializer


class ProductsTblDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = ProductsTbl.objects.all()
    serializer_class = ProductsTblSerializer




class UploadDetailView(CreateAPIView):
    queryset = ProductsTbl.objects.all()
    slug = queryset.objects.slug()
    thing = ProductsTbl.objects.get(slug=slug)
    Upload.objects.create(
        image=thing.cleaned_data['image'],
        description=thing.cleaned_data['description'],
        thing=thing,
    )

    queryset = thing.objects.save()
    serializer_class = 

UploadSerializers

......

我在终端控制台中遇到了错误

AttributeError: 'QuerySet' object has no attribute 'objects'

这是我的app / views.py形式,我可以用它上传图片确定

  

应用程序/ views.py

@login_required
def edit_thing_uploads(request, slug):
    # grab the object...
    thing = ProductsTbl.objects.get(slug=slug)
    # double checking just for security
    # if thing.user != request.user:
    #     raise Http404

    form_class = ProductsTblUploadForm
    # if we're coming to this view from a submitted form,
    if request.method == 'POST':
        # grab the data from the submitted form, # note the new "files" part
        form = form_class(
            data=request.POST,
            files=request.FILES,
            instance=thing
        )
        if form.is_valid():
            thing = form.save(commit=False)
            thing.useredit = request.user.username
            thing.save()
            # create a new object from the submitted form
            Upload.objects.create(
                image=form.cleaned_data['image'],
                description=form.cleaned_data['description'],
                thing=thing,
            )
            return redirect('edit_thing_uploads', slug=thing.slug)
    # otherwise just create the form
    else:
        form = form_class(instance=thing)

    # grab all the object's images
    uploads = thing.uploads.all()

    # and render the template
    return render(request, 'things/edit_thing_uploads.html', {
        'thing': thing,
        'form': form,
        'uploads': uploads,
    })

app / views.py的urls.py

  

项目/ urls.py

url(
        r'^things/(?P<slug>[-\w]+)/edit/images/$',
        views.edit_thing_uploads,
        name='edit_thing_uploads'
    ),
然而,当谈到django休息框架时,我只是不知道如何编写逻辑?

0 个答案:

没有答案