From c7efd64b96e8da9b19918e7ca86d27eed1228516 Mon Sep 17 00:00:00 2001 From: Jean Lionel Ndabaga <78383037+Lionel-hub1@users.noreply.github.com> Date: Fri, 8 Mar 2024 16:52:06 +0200 Subject: [PATCH] Add User model field and Type and Post models --- api/migrations/0001_initial.py | 53 ++++++++++++++++++++++++++++++++ api/models.py | 23 ++++++++++++++ api/urls.py | 5 +++ buffer_clone_backend/settings.py | 7 +++++ buffer_clone_backend/urls.py | 19 +++++++++++- 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 api/migrations/0001_initial.py create mode 100644 api/urls.py diff --git a/api/migrations/0001_initial.py b/api/migrations/0001_initial.py new file mode 100644 index 0000000..d1cc726 --- /dev/null +++ b/api/migrations/0001_initial.py @@ -0,0 +1,53 @@ +# Generated by Django 5.0.3 on 2024-03-08 14:50 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('email', models.EmailField(max_length=254, unique=True)), + ('username', models.CharField(max_length=100, unique=True)), + ('date_joined', models.DateTimeField(auto_now_add=True)), + ('last_login', models.DateTimeField(auto_now=True)), + ('is_admin', models.BooleanField(default=False)), + ('is_staff', models.BooleanField(default=False)), + ('is_active', models.BooleanField(default=True)), + ('is_superuser', models.BooleanField(default=False)), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='Type', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ], + ), + migrations.CreateModel( + name='Post', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('image', models.ImageField(blank=True, null=True, upload_to='images/')), + ('title', models.CharField(max_length=100)), + ('content', models.TextField()), + ('date_posted', models.DateTimeField(auto_now_add=True)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ('post_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.type')), + ], + ), + ] diff --git a/api/models.py b/api/models.py index c844fcb..5429d05 100644 --- a/api/models.py +++ b/api/models.py @@ -1,5 +1,6 @@ from django.db import models from django.contrib.auth.models import AbstractBaseUser +from django.conf import settings class User(AbstractBaseUser): @@ -12,6 +13,8 @@ class User(AbstractBaseUser): is_active = models.BooleanField(default=True) is_superuser = models.BooleanField(default=False) + USERNAME_FIELD = 'email' + def __str__(self): return f'{self.username} - {self.email}' @@ -20,3 +23,23 @@ class User(AbstractBaseUser): def has_module_perms(self, app_label): return True + + +class Type(models.Model): + name = models.CharField(max_length=100) + + def __str__(self): + return self.name + + +class Post(models.Model): + image = models.ImageField(upload_to='images/', null=True, blank=True) + title = models.CharField(max_length=100) + content = models.TextField() + post_type = models.ForeignKey(Type, on_delete=models.CASCADE) + date_posted = models.DateTimeField(auto_now_add=True) + author = models.ForeignKey( + settings.AUTH_USER_MODEL, on_delete=models.CASCADE) + + def __str__(self): + return self.title diff --git a/api/urls.py b/api/urls.py new file mode 100644 index 0000000..a3811ef --- /dev/null +++ b/api/urls.py @@ -0,0 +1,5 @@ +from django.urls import path + + +urlpatterns = [ +] diff --git a/buffer_clone_backend/settings.py b/buffer_clone_backend/settings.py index a92293c..7753e99 100644 --- a/buffer_clone_backend/settings.py +++ b/buffer_clone_backend/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/5.0/ref/settings/ """ from pathlib import Path +import os # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -37,6 +38,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'api', + 'rest_framework', ] MIDDLEWARE = [ @@ -119,6 +122,10 @@ USE_TZ = True STATIC_URL = 'static/' +# Media files +MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') + # Default primary key field type # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field diff --git a/buffer_clone_backend/urls.py b/buffer_clone_backend/urls.py index 3de4b65..d87f884 100644 --- a/buffer_clone_backend/urls.py +++ b/buffer_clone_backend/urls.py @@ -15,8 +15,25 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +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('api-auth/', include('rest_framework.urls')), + path('api/', include('api.urls')), ] + +if settings.DEBUG: + urlpatterns += static(settings.MEDIA_URL, + document_root=settings.MEDIA_ROOT) + +""" +#### Steps For Creating API #### + +Step 1: Create Model (For Creating database ) +Step 2: Create Serializer (For converting model to JSON) +Step 3: Create View (For creating API) +Step 4: Create URL (For mapping the API / Creating the endpoint) +"""