my problem is about makemigrations, it not detect fields and only creates id and foreignkey field. I added studentapp to INSTALLED_APP:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'studentapp.apps.StudentappConfig'
]
and models.py has: from django.db import models
class Department(models.Model):
name = models.CharField(null=True, ),
active = models.BooleanField(default=True),
def __str__(self):
return self.name
class Student(models.Model):
department = models.ForeignKey(Department, on_delete=models.CASCADE,
related_name="students", default=None)
name = models.CharField(max_length=50, default=None, null=True, blank=True),
date_born = models.DateField(),
hobbi= models.CharField(max_length=100,),
active = models.BooleanField(default=True),
def __str__(self):
return self.name
then I run command: python manage.py makemigrations studentapp
result of command:
Migrations for 'studentapp':
studentapp\migrations\0001_initial.py
- Create model Department
- Create model Student
newly created migration files (0001) only have id field and foreignkey field. code in migrations 0001:
# Generated by Django 4.0.3 on 2022-04-03 08:03
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Department',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Student',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('department', models.ForeignKey(default=None,
on_delete=django.db.models.deletion.CASCADE,
related_name='students', to='studentapp.department')),
],
),
]