I am new to Django but have some experience with MVC web development.
I am encountered with a weird problem, hopefully, it is only weird to me, not all folks.
After doing some changes (changing attribute’s names) in models project_name/models.py
. I ran: python manage.py makemigrations
to migrate the DB with new changes. But it showed that “No changes.”
So I decided to do a fresh re-migrate DB 🙁
- Delete DB and recreate empty DB
- Delete migrations/ folder and recreate folder with
__init__.py
- Re-run
makemigrations
OK. Now the weird thing happens.
No matter how I change in models.py
, the migrations/0001_initial.py
file always keep generate old attribute.
# models.py
class Task(models.Model):
STATUS_CHOICES = [
('PENDING', 'Pending'),
('PROCESSING', 'Processing'),
('COMPLETED', 'Completed'),
('FAILED', 'Failed'),
]
pdf_file = models.FileField(upload_to='pdfs/')
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='PENDING')
#Previously `preview_url`
preview_img = models.ImageField(blank=True, null=True, upload_to='previews/')
#Previously `thumbnail_url`
thumbnail_img = models.ImageField(blank=True, null=True, upload_to='thumbnails/')
error_message = models.TextField(blank=True, null=True)
uploaded_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"Task: {self.id} - {self.status}: {self.pdf_file.name}"
# migrations/0001_initial.py
...
operations = [
migrations.CreateModel(
name='Task',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('pdf_file', models.FileField(upload_to='uploads/')),
('status', models.CharField(choices=[('PENDING', 'Pending'), ('PROCESSING', 'Processing'), ('COMPLETED', 'Completed'), ('FAILED', 'Failed')], default='PENDING', max_length=10)),
('preview_url', models.URLField(blank=True, null=True)), # WEIRD
('thumbnail_url', models.URLField(blank=True, null=True)), # WEIRD
('error_message', models.TextField(blank=True, null=True)),
('uploaded_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
),
...
Why? and How to fix this?
Thanks in advance.