To develop a Django application to store and retrieve data from a Car Inventory Database using Object Relational Mapping(ORM).
Clone the problem from GitHub
Create a new app in Django project
Enter the code for admin.py and models.py
Execute Django admin and create details for 5 Car
models.py
from django.db import models
from django.contrib import admin
class Car(models.Model):
brand = models.CharField(max_length=100, help_text="Car Brand")
model_name = models.CharField(max_length=100, help_text="Model Name")
manufacture_year = models.IntegerField(help_text="Year of Manufacture")
car_type = models.CharField(max_length=50, help_text="Type of Car")
price = models.DecimalField(max_digits=10, decimal_places=2, help_text="Price in USD")
mileage = models.IntegerField(help_text="Mileage in km per liter")
color = models.CharField(max_length=30, help_text="Car Color")
engine_capacity = models.DecimalField(max_digits=4, decimal_places=1, help_text="Engine Capacity in Liters")
fuel_type = models.CharField(max_length=20, help_text="Fuel Type")
number_of_doors = models.IntegerField(help_text="Number of Doors")
seating_capacity = models.IntegerField(help_text="Seating Capacity")
class CarAdmin(admin.ModelAdmin):
list_display = ('brand', 'model_name', 'manufacture_year', 'car_type', 'price','mileage', 'color', 'engine_capacity', 'fuel_type', 'number_of_doors', 'color')
search_fields = ('brand', 'model_name', 'car_type', 'fuel_type', 'color')
admin.py
from django.contrib import admin
from .models import (Car,CarAdmin)
admin.site.register(Car,CarAdmin)
'''class CarAdmin(admin.ModelAdmin):
list_display = (
'brand',
'model_name',
'manufacture_year',
'car_type',
'price',
'fuel_type',
'transmission',
'color',
'engine_capacity',
'mileage',
'number_of_doors',
'registration_number'
)
search_fields = ('brand', 'model_name', 'car_type', 'fuel_type', 'transmission', 'color')
list_filter = ('brand', 'car_type', 'fuel_type', 'transmission', 'manufacture_year')
ordering = ('brand', 'model_name')'''
Thus the program for creating car inventory database database using ORM hass been executed successfully
.png)