From dbc47f761badbc54b713f81245a1defb0a20eee3 Mon Sep 17 00:00:00 2001 From: Roberamelaek Date: Thu, 17 Aug 2023 17:38:45 -0400 Subject: [PATCH] add rootuser and student for backend models --- api/serializers.py | 12 +++++++-- backend/admin.py | 3 +++ backend/migrations/0002_rootuser_student.py | 29 +++++++++++++++++++++ backend/models.py | 16 +++++++++++- backend/tests/test_models.py | 8 +++--- 5 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 backend/migrations/0002_rootuser_student.py diff --git a/api/serializers.py b/api/serializers.py index ff639876..cd9c66c6 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -1,7 +1,15 @@ from rest_framework import serializers -from backend.models import Event +from backend.models import Event, Student class EventSerializer(serializers.ModelSerializer): class Meta: model = Event - fields = ('id', 'title', 'content', 'student', 'mentor', 'start_date', 'end_date', 'status') \ No newline at end of file + fields = ('id', 'title', 'content', 'student', 'mentor', 'start_date', 'end_date', 'status') + +# the student class + +class StudentSerializer(serializers.ModelSerializer): + class Meta: + model = Student + fields = ('id', 'first_name','last_name','email') + diff --git a/backend/admin.py b/backend/admin.py index f73ae962..ae2562c1 100644 --- a/backend/admin.py +++ b/backend/admin.py @@ -13,3 +13,6 @@ class EventAdmin(admin.ModelAdmin): admin.site.register(models.Category) # Register your models here. + +admin.site.register(models.Student) + diff --git a/backend/migrations/0002_rootuser_student.py b/backend/migrations/0002_rootuser_student.py new file mode 100644 index 00000000..3914aea2 --- /dev/null +++ b/backend/migrations/0002_rootuser_student.py @@ -0,0 +1,29 @@ +# Generated by Django 4.1.2 on 2023-08-15 23:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('backend', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Rootuser', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ], + ), + migrations.CreateModel( + name='Student', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=100)), + ('last_name', models.CharField(max_length=100)), + ('email', models.EmailField(max_length=254, unique=True)), + ], + ), + ] diff --git a/backend/models.py b/backend/models.py index a7e2788f..07693b34 100644 --- a/backend/models.py +++ b/backend/models.py @@ -21,12 +21,26 @@ # name = models.CharField(max_length=100, blank=False, null=True) # phone = models.CharField(max_length=20, blank=True, null=True) + +class Rootuser(models.Model): + name = models.CharField(max_length=100) + class Category(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name - + +class Student(models.Model): + + first_name = models.CharField(max_length=100) + last_name = models.CharField(max_length=100) + email = models.EmailField(unique=True) + + + def __str__(self): + return f"{self.first_name} {self.last_name}" + # # create a model with two different user # class Mentor(models.Model): # user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='mentor') diff --git a/backend/tests/test_models.py b/backend/tests/test_models.py index 16f5ca20..9a4cefcd 100644 --- a/backend/tests/test_models.py +++ b/backend/tests/test_models.py @@ -1,6 +1,6 @@ from django.test import TestCase from django.contrib.auth.models import User -from backend.models import Event, Category +from backend.models import Event, Category, Student from django.utils import timezone @@ -41,8 +41,10 @@ def test_event_content(self): self.assertEqual(str(event), 'Event Title') self.assertEqual(str(category), 'django') - - + def test_student(self): + test_user = Student.objects.create(first_name='Robera',last_name='Melaek',email='email@gmail.com') + username = Student.objects.get(id=1) + assert(username.first_name,'Robera')