0

I am using Django RestFramework. I would like to if it is possible to upload a file and title using url parameter. For example,

http://127.0.0.1:8000/upload/title='some-title'&file_url='some-path'

Can anyone please guide as i am beginner in django rest framework.Here is my code:

Model:

class MyModel(models.Model):
    title = models.TextField(max_length=100, blank=True, default='No title', unique=True)
    file_url = models.FileField(upload_to='files/')
    created_at = models.DateTimeField(auto_now_add=True)

class Meta:
    ordering = ('created_at',)

Serializers:

class MySerializer(serializers.ModelSerializer):
    file_url = serializers.FileField(max_length=None, required=True)

class Meta:
    model = MyModel
    fields = ('id', 'title', 'file_url', 'created_at')

Views:

class MyViewSet(ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MySerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

class MyAPIView(generics.CreateAPIView):
    serializer_class = MySerializer
    permission_classes = (permissions.IsAdminUser,)

def get_queryset(self):

    title = self.lookup_field['title']
    file_path = self.lookup_field['file']
    obj = MyModel.objects.create(title=title, file_url=file_path)
    serializer = MySerializer(obj)
    return Response(serializer.data)

Urls:

router = DefaultRouter()
router.register(r'django', MyViewSet)

urlpatterns = [

url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^upload/(>P<title>[\w+])&?P(<file>[\w+])/$', MyAPIView.as_view()),
url(r'^', include(router.urls))
]

I want to get the result like:

enter image description here

Mirza715
  • 420
  • 5
  • 15
  • 1
    There are fixes for what you're trying to achieve but if you're planning to go to production, this will be very inefficient. Django may not be the best option to serve static/media files. You can use django-storages to store the file on aws s3! – Manan Mehta Jul 11 '17 at 23:14
  • @MananMehta so it means we cannot create/post data through url? – Mirza715 Jul 11 '17 at 23:17
  • You can but will be inefficient for a production server! – Manan Mehta Jul 11 '17 at 23:19
  • 1
    If you're persistent on doing what you're doing, I can go home and post some code to do this (in a couple hours). I think you'll have to first use native python code to download the file from the url and then pass it to the django file field! Look at https://stackoverflow.com/questions/1393202/django-add-image-in-an-imagefield-from-image-url and see if it helps – Manan Mehta Jul 11 '17 at 23:20
  • Thanks. i would appreciate! – Mirza715 Jul 11 '17 at 23:22
  • 1
    In the meanwhile, you can also look at the ProfilePictureUpload class at https://github.com/mehtamanan/MoviebookAPI/blob/master/userprofiles/views.py. It takes a file object, not a URL but it might help! – Manan Mehta Jul 11 '17 at 23:24

1 Answers1

1

If you want to send data through url like this, use query params instead of modifying your url regex like this.

Your url will simply be

url(r'^upload/$', MyAPIView.as_view())

on frontend you will append title and path in the url like this

localhost:8000/api/v1/upload?title=<your_title>&path=<your_file_path>

and then in your views you can simply extract these key value pairs from request.query_params

But I would advise not to use this for POST methods, where you can send all this data in request.data.

Sadan A.
  • 1,017
  • 1
  • 10
  • 28