Home > Back-end >  Django doesn't detect changes in my model
Django doesn't detect changes in my model

Time:11-11

I have an Orders model, in which I didn't have slug field before. So I decided to add a slug field to it. But when I run makemigrations command it says 'No changes detected'. I've tried to manually create migration and run it, but it didn't affect my database. Yes, I did check in settings, app is registered there. Model:

class Orders(models.Model):
    customer = models.ForeignKey('contractors.Customers',
                                 models.DO_NOTHING, blank=True, null=True)
    slug = models.SlugField(max_length=150, blank=True)
    employee = models.ForeignKey('employees.Employees',
                                 models.DO_NOTHING, blank=True, null=True)
    order_date = models.DateField(blank=True, null=True, auto_now=True)
    required_date = models.DateField(blank=True, null=True)
    shipped_date = models.DateField(blank=True, null=True)
    ship_via = models.ForeignKey('contractors.Shippers',
                                 models.DO_NOTHING, db_column='ship_via',
                                 blank=True, null=True)
    freight = models.FloatField(blank=True, null=True)
    ship_name = models.CharField(max_length=40, blank=True, null=True)
    ship_address = models.CharField(max_length=60, blank=True, null=True)
    ship_city = models.CharField(max_length=15, blank=True, null=True)
    ship_region = models.CharField(max_length=15, blank=True, null=True)
    ship_postal_code = models.CharField(max_length=10, blank=True, null=True)
    ship_country = models.CharField(max_length=15, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'orders'

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.customer   "-"   str(self.order_date))
        super(Orders, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return u'/orders/%d' % self.pk

    def __str__(self):
        return f"{self.customer} {self.order_date}"

My attempt to manually create migration:

# Generated by Django 3.2.7 on 2021-11-10 13:47

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('orders', '0008_alter_orderdetails_options'),
    ]

    operations = [
        migrations.AddField(
            model_name='orders',
            name='slug',
            field=models.SlugField(max_length=150, blank=True),
            preserve_default=False,
        ),
    ]

CodePudding user response:

You have set managed = False in your model Meta. Django will not manage the model for you, see here. Set it to True if you want Django to manage migrations for you.

CodePudding user response:

problem

class Meta:
        managed = False
        db_table = 'orders'

solution

class Meta:
   db_table = 'orders'
  • Related