django rest api with this model : class VehicleDetail(models.Model): vehicletype =…
- User
django rest api with this model : class VehicleDetail(models.Model): vehicle_type = models.CharField(max_length=100) vehicle_registration_number = models.CharField(max_length=100) brand_name = models.CharField(max_length=100) model_year = models.CharField(max_length=100) engine_number = models.CharField(max_length=100) station = models.ForeignKey(StationDetail, on_delete=models.CASCADE) created_date = models.DateTimeField(auto_now_add=True)
- Assistant
To create a Django REST API for a model like
VehicleDetail, you need to set up serializers, views, and URLs. Below is a step-by-step guide on how to do this:Step 1: Set up your Django project and app
First, make sure you have a Django project and an application set up.
django-admin startproject myproject cd myproject python manage.py startapp myappStep 2: Define your models
Ensure your
VehicleDetailmodel is defined inmodels.pyin your application directory (myapp/models.py).from django.db import models class VehicleDetail(models.Model): vehicle_type = models.CharField(max_length=100) vehicle_registration_number = models.CharField(max_length=100) brand_name = models.CharField(max_length=100) model_year = models.CharField(max_length=100) engine_number = models.CharField(max_length=100) station = models.ForeignKey('StationDetail', on_delete=models.CASCADE) created_date = models.DateTimeField(auto_now_add=True) class StationDetail(models.Model): # Your fields for StationDetail model here name = models.CharField(max_length=100)Step 3: Create serializers
Create a
serializers.pyfile in your application directory (myapp/serializers.py) and define a serializer for yourVehicleDetailmodel.from rest_framework import serializers from .models import VehicleDetail class VehicleDetailSerializer(serializers.ModelSerializer): class Meta: model = VehicleDetail fields = '__all__'Step 4: Create views
Create a
views.pyfile in your application directory (myapp/views.py) and define API views.from rest_framework import generics from .models import VehicleDetail from .serializers import VehicleDetailSerializer class VehicleDetailListCreate(generics.ListCreateAPIView): queryset = VehicleDetail.objects.all() serializer_class = VehicleDetailSerializer class VehicleDetailRetrieveUpdateDestroy(generics.RetrieveUpdateDestroyAPIView): queryset = VehicleDetail.objects.all() serializer_class = VehicleDetailSerializerStep 5: Set up URLs
In your application directory, create a
urls.pyfile (myapp/urls.py) and set up the URLs for your API views.from django.urls import path from .views import VehicleDetailListCreate, VehicleDetailRetrieveUpdateDestroy urlpatterns = [ path('vehicle-details/', VehicleDetailListCreate.as_view(), name='vehicle-detail-list-create'), path('vehicle-details/<int:pk>/', VehicleDetailRetrieveUpdateDestroy.as_view(), name='vehicle-detail-retrieve-update-destroy'), ]Then include these URLs in your project's main
urls.pyfile (myproject/urls.py).from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('api/', include('myapp.urls')), ]Step 6: Migrate the database
Make sure to create and apply your migrations to update the database schema.
python manage.py makemigrations python manage.py migrateStep 7: Add the app to INSTALLED_APPS
Make sure your app and Django REST framework are added to
INSTALLED_APPSinsettings.py.INSTALLED_APPS = [ ... 'rest_framework', 'myapp', ]Step 8: Run the server
Finally, you can run the server and test your API endpoints.
python manage.py runserverNow you should have a working Django REST API for the
VehicleDetailmodel. You can perform CRUD (Create, Read, Update, Delete) operations on thevehicle-details/endpoint.