Skip to content

Commit 5b3f334

Browse files
committed
models
1 parent a98fa54 commit 5b3f334

File tree

9 files changed

+122
-23
lines changed

9 files changed

+122
-23
lines changed

server/djangoapp/admin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@
1111
# CarMakeAdmin class with CarModelInline
1212

1313
# Register models here
14+
15+
from django.contrib import admin
16+
from .models import CarMake, CarModel
17+
18+
# Registering models with their respective admins
19+
admin.site.register(CarMake)
20+
admin.site.register(CarModel)

server/djangoapp/apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.apps import AppConfig
22

3-
43
class DjangoappConfig(AppConfig):
4+
default_auto_field = 'django.db.models.BigAutoField'
55
name = 'djangoapp'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Generated by Django 5.2.8 on 2025-11-29 12:07
2+
3+
import django.core.validators
4+
import django.db.models.deletion
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
initial = True
11+
12+
dependencies = [
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='CarMake',
18+
fields=[
19+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('name', models.CharField(max_length=100)),
21+
('description', models.TextField()),
22+
],
23+
),
24+
migrations.CreateModel(
25+
name='CarModel',
26+
fields=[
27+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
28+
('name', models.CharField(max_length=100)),
29+
('type', models.CharField(choices=[('SEDAN', 'Sedan'), ('SUV', 'SUV'), ('WAGON', 'Wagon')], default='SUV', max_length=10)),
30+
('year', models.IntegerField(default=2023, validators=[django.core.validators.MaxValueValidator(2023), django.core.validators.MinValueValidator(2015)])),
31+
('car_make', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='djangoapp.carmake')),
32+
],
33+
),
34+
]

server/djangoapp/migrations/__init__.py

Whitespace-only changes.

server/djangoapp/models.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
# Uncomment the following imports before adding the Model code
1+
from django.db import models
2+
from django.core.validators import MaxValueValidator, MinValueValidator
23

3-
# from django.db import models
4-
# from django.utils.timezone import now
5-
# from django.core.validators import MaxValueValidator, MinValueValidator
64

5+
class CarMake(models.Model):
6+
name = models.CharField(max_length=100)
7+
description = models.TextField()
78

8-
# Create your models here.
9+
def __str__(self):
10+
return self.name
911

10-
# <HINT> Create a Car Make model `class CarMake(models.Model)`:
11-
# - Name
12-
# - Description
13-
# - Any other fields you would like to include in car make model
14-
# - __str__ method to print a car make object
1512

13+
class CarModel(models.Model):
14+
car_make = models.ForeignKey(CarMake, on_delete=models.CASCADE)
15+
name = models.CharField(max_length=100)
1616

17-
# <HINT> Create a Car Model model `class CarModel(models.Model):`:
18-
# - Many-To-One relationship to Car Make model (One Car Make has many
19-
# Car Models, using ForeignKey field)
20-
# - Name
21-
# - Type (CharField with a choices argument to provide limited choices
22-
# such as Sedan, SUV, WAGON, etc.)
23-
# - Year (IntegerField) with min value 2015 and max value 2023
24-
# - Any other fields you would like to include in car model
25-
# - __str__ method to print a car make object
17+
CAR_TYPES = [
18+
('SEDAN', 'Sedan'),
19+
('SUV', 'SUV'),
20+
('WAGON', 'Wagon'),
21+
]
22+
23+
type = models.CharField(max_length=10, choices=CAR_TYPES, default='SUV')
24+
25+
year = models.IntegerField(
26+
default=2023,
27+
validators=[
28+
MaxValueValidator(2023),
29+
MinValueValidator(2015)
30+
]
31+
)
32+
33+
def __str__(self):
34+
return self.name

server/djangoapp/populate.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
1+
from .models import CarMake, CarModel
2+
13
def initiate():
2-
print("Populate not implemented. Add data manually")
4+
car_make_data = [
5+
{"name":"NISSAN", "description":"Great cars. Japanese technology"},
6+
{"name":"Mercedes", "description":"Great cars. German technology"},
7+
{"name":"Audi", "description":"Great cars. German technology"},
8+
{"name":"Kia", "description":"Great cars. Korean technology"},
9+
{"name":"Toyota", "description":"Great cars. Japanese technology"},
10+
]
11+
12+
car_make_instances = []
13+
for data in car_make_data:
14+
car_make_instances.append(CarMake.objects.create(name=data['name'], description=data['description']))
15+
16+
17+
# Create CarModel instances with the corresponding CarMake instances
18+
car_model_data = [
19+
{"name":"Pathfinder", "type":"SUV", "year": 2023, "car_make":car_make_instances[0]},
20+
{"name":"Qashqai", "type":"SUV", "year": 2023, "car_make":car_make_instances[0]},
21+
{"name":"XTRAIL", "type":"SUV", "year": 2023, "car_make":car_make_instances[0]},
22+
{"name":"A-Class", "type":"SUV", "year": 2023, "car_make":car_make_instances[1]},
23+
{"name":"C-Class", "type":"SUV", "year": 2023, "car_make":car_make_instances[1]},
24+
{"name":"E-Class", "type":"SUV", "year": 2023, "car_make":car_make_instances[1]},
25+
{"name":"A4", "type":"SUV", "year": 2023, "car_make":car_make_instances[2]},
26+
{"name":"A5", "type":"SUV", "year": 2023, "car_make":car_make_instances[2]},
27+
{"name":"A6", "type":"SUV", "year": 2023, "car_make":car_make_instances[2]},
28+
{"name":"Sorrento", "type":"SUV", "year": 2023, "car_make":car_make_instances[3]},
29+
{"name":"Carnival", "type":"SUV", "year": 2023, "car_make":car_make_instances[3]},
30+
{"name":"Cerato", "type":"Sedan", "year": 2023, "car_make":car_make_instances[3]},
31+
{"name":"Corolla", "type":"Sedan", "year": 2023, "car_make":car_make_instances[4]},
32+
{"name":"Camry", "type":"Sedan", "year": 2023, "car_make":car_make_instances[4]},
33+
{"name":"Kluger", "type":"SUV", "year": 2023, "car_make":car_make_instances[4]},
34+
# Add more CarModel instances as needed
35+
]
36+
37+
for data in car_model_data:
38+
CarModel.objects.create(name=data['name'], car_make=data['car_make'], type=data['type'], year=data['year'])

server/djangoapp/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
path(route='login', view=views.login_user, name='login'),
1313
path(route='logout', view=views.logout_request, name='logout'),
1414
path(route='register', view=views.registration, name='register'),
15+
path(route='get_cars', view=views.get_cars, name ='getcars'),
1516

1617
# path for dealer reviews view
1718

server/djangoapp/views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,25 @@
1515
from django.views.decorators.csrf import csrf_exempt
1616
from .populate import initiate
1717

18+
from .models import CarMake, CarModel
1819

1920
# Get an instance of a logger
2021
logger = logging.getLogger(__name__)
2122

2223

2324
# Create your views here.
2425

26+
def get_cars(request):
27+
count = CarMake.objects.filter().count()
28+
print(count)
29+
if(count == 0):
30+
initiate()
31+
car_models = CarModel.objects.select_related('car_make')
32+
cars = []
33+
for car_model in car_models:
34+
cars.append({"CarModel": car_model.name, "CarMake": car_model.car_make.name})
35+
return JsonResponse({"CarModels":cars})
36+
2537
# Create a `login_request` view to handle sign in request
2638
@csrf_exempt
2739
def login_user(request):

server/djangoproj/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
# SECURITY WARNING: don't run with debug turned on in production!
2929
DEBUG = True
3030

31-
ALLOWED_HOSTS=['localhost','https://baamranemoua-8000.theianext-1-labs-prod-misc-tools-us-east-0.proxy.cognitiveclass.ai/']
32-
CSRF_TRUSTED_ORIGINS=['https://baamranemoua-8000.theianext-1-labs-prod-misc-tools-us-east-0.proxy.cognitiveclass.ai/']
31+
ALLOWED_HOSTS=['localhost','https://baamranemoua-8000.theiadockernext-0-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai']
32+
CSRF_TRUSTED_ORIGINS=['https://baamranemoua-8000.theiadockernext-0-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai']
3333

3434
REST_FRAMEWORK = {
3535
'DEFAULT_AUTHENTICATION_CLASSES': [],

0 commit comments

Comments
 (0)