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

6
  • 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! Commented Jul 11, 2017 at 23:14
  • @MananMehta so it means we cannot create/post data through url? Commented Jul 11, 2017 at 23:17
  • You can but will be inefficient for a production server! Commented Jul 11, 2017 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 stackoverflow.com/questions/1393202/… and see if it helps Commented Jul 11, 2017 at 23:20
  • 1
    In the meanwhile, you can also look at the ProfilePictureUpload class at github.com/mehtamanan/MoviebookAPI/blob/master/userprofiles/…. It takes a file object, not a URL but it might help! Commented Jul 11, 2017 at 23:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.