Django Rest Framework: No Models


Facebook facebook Rahul Nama to contact

I am creating a REST API using DRF (Django Rest Framework). The API just takes the user's twitter handle and returns their twitter data.

Here, I'm not using a model here because it's not required.

Should I be using a serializer here? If yes, why? Now I can return data without using serializer. Also, "My API" cannot be browsed over the web. How should I make it web-browsable: This is one of the best features of DRF.

Edit: 1

I use function in view.

@api_view(['GET'])
@csrf_exempt
def getdetails(request):

    call the twitter api
    receive the data
    return HttpResponse(JsonResponse( {'data': {'twitter_id':id,'tweets':count,'Followers':followers,'following':count_follow}}))

In the browser, I only see JSON data like this.

{"data":{"twitter_id":352525,"tweets":121,"followers":1008,"followers":281}}

JPG format

you can use the Serializerresult

class SampleSerializer(serializers.Serializer):
    field1 = serializers.CharField()
    field2 = serializers.IntegerField()
    # ... other fields

usage

my_data = {
    "field1": "my sample",
    "field2": 123456
}

my_serializer = SampleSerializer(data=my_data)
my_serializer.is_valid(True)
data = my_serializer.data

You will get serialized data in datavariables (which can be used my_serializer.datadirectly )

Should I be using a serializer here?

It's up to you, because if you want the response JSON to be displayed without any modification to the Twitter API, you can do without the DRF serializer. And if you wish to do some formatting of the JSON, my answer will help you

My API cannot be browsed over the web. How should I make it web-browsable?

Maybe you are following the wrong procedure. Anyway, we can't say more about this without seeing your code snippet

Update 1

from rest_framework.response import Response


@api_view(['GET'])
@csrf_exempt
def getdetails(request):
    call the twitter  api
    twitter_api = get_response_from_twitter()  # Json response
    return Response(data=twitter_api)

Related


Django Rest Framework: No Models

Facebook facebook Rahul Nama to contact I am creating a REST API using DRF (Django Rest Framework). The API just takes the user's twitter handle and returns their twitter data. Here, I'm not using a model here because it's not required. Should I be using a ser

Multiple models in Django Rest Framework?

Coderaemon: I am using Django Rest Framework . I want to serialize multiple models and send as response. Currently, only one model can be sent per view (as CartViewshown below, only Cart objects are sent). The following models (unrelated) can be there. class S

Multiple models in Django Rest Framework?

Coderaemon: I am using Django Rest Framework . I want to serialize multiple models and send as response. Currently, only one model can be sent per view (as CartViewshown below, only Cart objects are sent). The following models (unrelated) can be there. class S

Multiple models in Django Rest Framework?

Coderaemon I am using Django Rest Framework . I want to serialize multiple models and send as response. Currently I can only send one model per view (as CartViewbelow, only the Cart object). The following models (unrelated) can be there. class Ship_address(mod

Multiple models in Django Rest Framework?

Coderaemon: I am using Django Rest Framework . I want to serialize multiple models and send as response. Currently, only one model can be sent per view (as CartViewshown below, only Cart objects are sent). The following models (unrelated) can be there. class S

Django REST Framework serializer without models

Farid El Nasire: I'm working on several endpoints that aggregate data. For example, one of the endpoints will return an array of objects, one for each day, and it will have the number of comments, likes, and photos posted by a particular user. The object has a

Django REST Framework: save related models in ModelViewSet

daveoncode I'm trying to figure out how to save related models using Django REST framework. In my app I have a model Recipewith 2 related models: RecipeIngredientand RecipeStep. An Recipeobject must have at least 3 related RecipeIngredientand 3 RecipeStep. Bef

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Serialize related models in Django Rest Framework

Newt I have django models, simplified to: class Client(models.Model): name = models.CharField(max_length=255) def __str__(self): return self.name class ClientDetail(models.Model): client = models.ForeignKey(Client, on_delete=models.CASCA

Django REST Framework: save related models in ModelViewSet

daveoncode I'm trying to figure out how to save related models using Django REST framework. In my app I have a model Recipewith 2 related models: RecipeIngredientand RecipeStep. An Recipeobject must have at least 3 related RecipeIngredientand 3 RecipeStep. Bef

Nested serializers "through models" in Django Rest Framework

bdex I'm having trouble serializing an intermediate "pivot" model and attaching it to each item in a "many-to-many" relationship in Django Rest Framework. example: models.py: class Member(models.Model): name = models.CharField(max_length = 20) groups =

Django rest framework models not visible in admin site

Johan Khanye I'm using a simple DRF API and although I can access some models from the same application, I can't seem to access one of the many models from the admin site, please help! project/app/models.py looks like this: class ImportantModel(models.Model):

Django rest framework serializes models using FK

Rocky Quentin I am learning Django and Vuejs. I don't understand how it works. I have a "Nc" model which has some foreign keys to other models like Affaire. How can I get the instance 's str return value instead of the actual value in the JSON file... models.p

Django rest framework serializes models using FK

Rocky Quentin I am learning Django and Vuejs. I don't understand how it works. I have a "Nc" model which has some foreign keys to other models like Affaire. How can I get the instance 's str return value instead of the actual value in the JSON file... models.p

Detect changes to related models in Django Rest Framework

sonar ford I have a model in django, let's call it, which is referenced productby multiple purchaseinstances . Purchases can be made for any number of items product, subject to the following restrictions: The total of all items in all purchases for a given pro

Django rest framework models not visible in admin site

Johan Khanye I'm using a simple DRF API and although I can access some models from the same application, I can't seem to access one of the many models from the admin site, please help! project/app/models.py looks like this: class ImportantModel(models.Model):

Django rest framework serializes models using FK

Rocky Quentin I am learning Django and Vuejs. I don't understand how it works. I have a "Nc" model which has some foreign keys to other models like Affaire. How can I get the instance 's str return value instead of the actual value in the JSON file... models.p

Django Rest Framework, querying with multiple models

Ben2pop I'm very new to using REST frameworks and I didn't find in tutorials how to achieve what I'm looking for; In my serialiser.py I have a class for serializing MyUser model class MyUserSerializer(ModelSerializer): class Meta: model = MyUser

Django REST Framework serializer without models

Farid El Nasire: I'm working on several endpoints that aggregate data. For example, one of the endpoints will return an array of objects, one for each day, and it will have the number of comments, likes, and photos posted by a particular user. The object has a

Django REST Framework: save related models in ModelViewSet

daveoncode I'm trying to figure out how to save related models using Django REST framework. In my app I have a model Recipewith 2 related models: RecipeIngredientand RecipeStep. An Recipeobject must have at least 3 related RecipeIngredientand 3 RecipeStep. Bef

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Search tables for multiple models in Django Rest Framework

DjangoNoob I have 3 tables PC(ID, PcNAME, Brand) , CellPhoness(ID, CellPhoneName, Brand) , Printers(ID, PrinterName, Brand) . There is no relationship between these three tables. I want to run a query where the user can enter a search string and the program wi

django rest framework nested fields with multiple models

Momokjaaaaa This is django and django rest framework. I have 2 models: User and Phone. first question: I want to be able to update user data (email) as well as phone data (phone number) in 1 api update response. Phone numbers can be 0 or many. Well, it's actua

Django REST API framework without models

jelly Bean I created a simple html page that allows me to upload images and store them in a directory without using models. My goal is to use the REST API framework and display a specific image uploaded in the REST API, i.e. {"image": "uploaded_image_by_user"}

Nested serializers "through models" in Django Rest Framework

bdex I'm having trouble serializing an intermediate "pivot" model and attaching it to each item in a "many-to-many" relationship in Django Rest Framework. example: models.py: class Member(models.Model): name = models.CharField(max_length = 20) groups =

Serialize related models in Django Rest Framework

Newt I have django models, simplified to: class Client(models.Model): name = models.CharField(max_length=255) def __str__(self): return self.name class ClientDetail(models.Model): client = models.ForeignKey(Client, on_delete=models.CASCA

Django REST API framework without models

jelly Bean I created a simple html page that allows me to upload images and store them in a directory without using models. My goal is to use the REST API framework and display a specific image uploaded in the REST API, i.e. {"image": "uploaded_image_by_user"}

Django rest framework models not visible in admin site

Johan Khanye I'm using a simple DRF API and although I can access some models from the same application, I can't seem to access one of the many models from the admin site, please help! project/app/models.py looks like this: class ImportantModel(models.Model):

Django rest framework serializes models using FK

Rocky Quentin I am learning Django and Vuejs. I don't understand how it works. I have a "Nc" model which has some foreign keys to other models like Affaire. How can I get the instance 's str return value instead of the actual value in the JSON file... models.p