Pipfile์ ๋๋ค. ๊ด๋ จ ํจํค์ง๋ค์ด ์ ์๋์ด ์๋๋ฐ์. ๊ฐ๋ณ๊ฒ ๊น์์ฃผ๊ณ ์์ํ ๊ฒ์.
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
flake8 = "*"
black = "*"
[packages]
django = "*"
pillow = "*"
django-seed = "*"
[requires]
python_version = "3.7"
[pipenv]
allow_prereleases = true
๋ฃจํธ ์ค์ ํ๊ฒฝ์์ url์ค์ ์ด ์ด๋ฃจ์ด์ง๋๋ฐ์. ์๋ ๋ฏธ๋์ด ํ์ผ์ ์์น์ ๋ํ ๋งค๊ฐ๋ณ์ ์ ์์ endpoint๊น์ง settings.py๋ฅผ ํตํด ์ ๋ฌ ๋๋๊ฒ ๋ณด์ด๋ค์.
๊ด๋ จ ์์ค ์ฝ๋ ๋ฐ ํ์ผ๋ค์ด ์๋ ๊นํ ์ฃผ์์
๋๋ค.
https://github.com/hyeseong-dev/2021-03-11_airbnb-api
config/urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path("admin/", admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
๋น ๋ฅด๊ฒ models.py ์ฝ๋๋ฅผ ์ ์ ํ ํ ๋ฐ์.
room์ฑ
from django.db import models
from core.models import CoreModel
class Room(CoreModel):
name = models.CharField(max_length=140)
address = models.CharField(max_length=140)
price = models.IntegerField(help_text="USD per night")
beds = models.IntegerField(default=1)
lat = models.DecimalField(max_digits=10, decimal_places=6)
lng = models.DecimalField(max_digits=10, decimal_places=6)
bedrooms = models.IntegerField(default=1)
bathrooms = models.IntegerField(default=1)
check_in = models.TimeField(default="00:00:00")
check_out = models.TimeField(default="00:00:00")
instant_book = models.BooleanField(default=False)
user = models.ForeignKey(
"users.User", on_delete=models.CASCADE, related_name="rooms"
)
def __str__(self):
return self.name
def photo_number(self):
return self.photos.count()
photo_number.short_description = "Photo Count"
class Photo(CoreModel):
file = models.ImageField()
room = models.ForeignKey(
"rooms.Room", related_name="photos", on_delete=models.CASCADE
)
caption = models.CharField(max_length=140)
def __str__(self):
return self.room.name
user์ฑ
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
avatar = models.ImageField(upload_to="avatars", blank=True)
superhost = models.BooleanField(default=False)
favs = models.ManyToManyField("rooms.Room", related_name="favs")
def room_count(self):
return self.rooms.count()
room_count.short_description = "Room Count"
users/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from . import models
@admin.register(models.User)
class UserAdmin(UserAdmin):
fieldsets = UserAdmin.fieldsets + (
("Custom Profile", {"fields": ("avatar", "superhost",)},),
)
list_display = UserAdmin.list_display + ("room_count",)
users/admin.py
๊ด๋ฆฌ์ ํ์ด์ง์์ ์์ฑ๋ ์ ์ ๋ค์ GUI ์ธํฐํ์ด์ค๋ฅผ ํตํด์ ๊ด๋ฆฌํ ์ ์์ด์.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from . import models
@admin.register(models.User)
class UserAdmin(UserAdmin):
fieldsets = UserAdmin.fieldsets + (
("Custom Profile", {"fields": ("avatar", "superhost",)},),
)
list_display = UserAdmin.list_display + ("room_count",)
...
...
...
DJANGO_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
PROJECT_APPS = [
"core.apps.CoreConfig",
"users.apps.UsersConfig",
"rooms.apps.RoomsConfig",
]
THIRD_PARTY_APPS = []
INSTALLED_APPS = DJANGO_APPS + PROJECT_APPS + THIRD_PARTY_APPS
...
...
...
STATIC_URL = "/static/"
MEDIA_ROOT = os.path.join(BASE_DIR, "uploads")
MEDIA_URL = "/media/"
# Auth
AUTH_USER_MODEL = "users.User"
django-seed ํจํค์ง๋ฅผ ์ด์ฉํ์ฌ dummy๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด์ผ API ๊ฒฐ๊ณผ๋ฌผ์ ํ์ธ ํ ์ ์์ด์.
๋ ๋ํ
์ผํ ๊ฒ๋ค์ ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ ๊ฒ์.
https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/
๊ฒฝ๋ก core/management/commands/mega_seed.py
import random
from datetime import datetime
from django.core.management.base import BaseCommand
from django_seed import Seed
from users.models import User
from rooms.models import Room, Photo
class Command(BaseCommand):
help = "It seeds the DB with tons of stuff"
def handle(self, *args, **options):
user_seeder = Seed.seeder()
user_seeder.add_entity(User, 20, {"is_staff": False, "is_superuser": False})
user_seeder.execute()
users = User.objects.all()
room_seeder = Seed.seeder()
room_seeder.add_entity(
Room,
150,
{
"user": lambda x: random.choice(users),
"name": lambda x: room_seeder.faker.street_address(),
"price": lambda x: random.randint(0, 300),
"beds": lambda x: random.randint(0, 5),
"bedrooms": lambda x: random.randint(0, 3),
"bathrooms": lambda x: random.randint(0, 5),
"instant_book": lambda x: random.choice([True, False]),
"check_in": lambda x: datetime.now(),
"check_out": lambda x: datetime.now(),
},
)
room_seeder.execute()
rooms = Room.objects.all()
for room in rooms:
for i in range(random.randint(5, 10)):
Photo.objects.create(
caption=room_seeder.faker.sentence(),
room=room,
file=f"room_photos/{random.randint(1, 31)}.webp",
)
self.stdout.write(self.style.SUCCESS(f"Everything seeded"))
์ฅ๊ณ ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค๊ธฐ ์ด์ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ๊ทธ๋ฆ์ธ ๋๋น๋ฅผ ๋ง๋ค๊ธฐ ์ํด ๋ง์ด๊ทธ๋ ์ด์ ๋ช ๋ น์ด๋ฅผ ๋๋ฆฌ๋๊ฑฐ ์์ง ๋ง์ธ์.
์ผ๋ฐ์ ์ธ ์ฅ๊ณ ๋ทฐ ๋ก์ง!
from django.core import serializers
from django.http import HttpResponse
from rooms.models import Room
def list_rooms(request):
data = serializers.serialize("json", Room.objects.all())
response = HttpResponse(content=data)
return response
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path("admin/", admin.site.urls),
path("rooms/", include("core.urls")),
]
if settings.DEBUG:
from django.urls import path
from . import views
app_name = "core"
urlpatterns = [
path("list", views.list_rooms)
]