में मल्टीपार्ट अपलोड फ़ाइल है कि पदों दोनों json और एक फ़ाइल बहुखण्डीय का उपयोग कर।एक टेस्ट दोनों JSON समर्थन और मैं अपने <a href="http://www.django-rest-framework.org/" rel="noreferrer">DRF</a> एप्लिकेशन के लिए एक परीक्षण लिखने के लिए चाहते हैं डीआरएफ
यह मैंने अभी तक कोशिश की है लेकिन collection_items
(निर्माण विधि में) खाली है। क्या मुझे यह काम सही तरीके से करने के लिए मेरे विचार को संशोधित करने की आवश्यकता है, या क्या मैं नीचे दिए गए अपने परीक्षण मामले में कुछ गलत तरीके से कर रहा हूं?
मेरे टेस्ट:
class Collection(models.Model):
title = models.CharField(max_length=60)
collection_items = models.ManyToManyField('collection.Item')
class Item(models.Model):
image = models.ImageField(upload_to="/",null=True, blank=True)
serializers:
class ItemCollectionDetailSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = ('id', 'image')
read_only_fields = ('image',)
class CollectionListSerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='col_detail')
collection_items = ItemCollectionDetailSerializer(many=True, required=True)
class Meta:
model = Collection
fields = ('url', 'id', 'collection_items')
def create(self, validated_data):
item_data = validated_data.pop('collection_items')
print(item_data) # <----- **EMPTY HERE???**
etc ....edited for brevity
image = Image.new('RGB', (100, 100))
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
image.save(tmp_file)
files = {"collection_items": [{"image": tmp_file}]}
payload = json.dumps({
"title": "Test Collection",
})
self.api_factory.credentials(Authorization='Bearer ' + self.token)
response = self.api_factory.post(url, data=payload, files=files, format='multipart')
यह मॉडल हैतो print(item_data)
खाली है [], क्यों? मैं इसे कैसे हल करूं?
यह मेरा पूरा दृश्य है: नीचे, क्या मुझे यहां कुछ करने की ज़रूरत है?
class CollectionListView(generics.ListCreateAPIView):
queryset = Collection.objects.all()
serializer_class = CollectionListSerializer
मैं Django Rest Framework 3.x, Django 1.8.x और पायथन 3.4.x का उपयोग कर रहा हूं।
अद्यतन
मैं नीचे की कोशिश की है, लेकिन अभी भी कोई खुशी! collection_items
मेरे create
में खाली है। यह या तो इस तथ्य के साथ करना है कि यह एक घोंसला वाली वस्तु है या मेरे विचार में कुछ होना है।
stream = BytesIO()
image = Image.new('RGB', (100, 100))
image.save(stream, format='jpeg')
uploaded_file = SimpleUploadedFile("temp.jpeg", stream.getvalue())
payload = {
"title": "Test Collection",
"collection_items": [{"image": uploaded_file}],
}
self.api_factory.credentials(Authorization='Bearer ' + self.test_access.token)
response = self.api_factory.post(url, data=payload, format='multipart')
अद्यतन 2
अगर मैं json.dumps
उपयोग करने के लिए यह अब फ़ाइल लेकिन निश्चित रूप से यह काम नहीं कर सकते हैं देखने के लिए लगता है मेरी पेलोड को बदलने!
payload = json.dumps({
"title": "Test Collection",
"collection_items": [{"image": uploaded_file}],
})
त्रुटि
<SimpleUploadedFile: temp.jpeg (text/plain)> is not JSON serializable
पुनश्च
मैं जानता हूँ कि फ़ाइल को अपलोड किया जा रहा है क्योंकि अगर मैं अपने serializer में निम्न कार्य करें ...
print(self.context.get("request").data['collection_items'])
मैं
{'image': <SimpleUploadedFile: temp.jpeg (text/plain)>}
हाय, मैंने अपने ओपी को इस उत्तर के आधार पर थक गया है और यह अभी भी खाली है! :( – Prometheus
ध्यान दें कि फ़ील्ड छवि केवल आइटमकोलेक्शनडिंटसेरियलाइज़र में पढ़ने के रूप में सेट की गई है। किसी भी तरह से मैं आपको गलत होने के बारे में जानने के लिए पीडीबी का उपयोग करके धारावाहिक सत्यापन विधि पर कदम उठाने की सलाह देता हूं। – dukebody