Added countries app with get views (filter view and get by alpha2 view)

This commit is contained in:
ITQ
2024-02-27 21:56:49 +03:00
parent 607f6de88c
commit a2253a12c5
14 changed files with 108 additions and 16 deletions
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class CountriesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "countries"
@@ -0,0 +1,24 @@
# Generated by Django 4.2.10 on 2024-02-27 18:38
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Country',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('alpha2', models.CharField(max_length=2)),
('alpha3', models.CharField(max_length=3)),
('region', models.CharField()),
],
),
]
+11
View File
@@ -0,0 +1,11 @@
from django.db import models
class Country(models.Model):
name = models.CharField(max_length=100)
alpha2 = models.CharField(max_length=2)
alpha3 = models.CharField(max_length=3)
region = models.CharField()
def __str__(self):
return self.name
+9
View File
@@ -0,0 +1,9 @@
from rest_framework import serializers
from countries.models import Country
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = ["name", "alpha2", "alpha3", "region"]
+12
View File
@@ -0,0 +1,12 @@
from django.urls import path
import countries.views
urlpatterns = [
path("", countries.views.CountryListView.as_view(), name="countries"),
path(
"<str:alpha2>/",
countries.views.CountryByAlpha2View.as_view(),
name="country_by_alpha2",
),
]
+16
View File
@@ -0,0 +1,16 @@
from rest_framework.generics import ListAPIView, RetrieveAPIView
from countries.models import Country
from countries.serializers import CountrySerializer
class CountryListView(ListAPIView):
queryset = Country.objects.all().order_by("alpha2")
filterset_fields = ["region"]
serializer_class = CountrySerializer
class CountryByAlpha2View(RetrieveAPIView):
queryset = Country.objects.all()
serializer_class = CountrySerializer
lookup_field = "alpha2"