How to remove default value function in Django

In the commentary on the answer to this question, I asked how to remove a field with a default value. To summarize, example code:

def get_deadline():
    return datetime.today() + timedelta(days=20)

class Bill(models.Model):
    name = models.CharField(max_length=50)
    customer = models.ForeignKey(User, related_name='bills')
    date = models.DateField(default=datetime.today)
    deadline = models.DateField(default=get_deadline)

and my question to the code:

How to delete a field deadlineagain and also delete a get_deadlinefunction? I deleted the field with the function for the default value, but now Django crashes at startup after deleting the function. I could manually change the migration, which would be normal in this case, but what if you just changed the default function and wanted to delete the old function?

I ended up deleting the default part related to it, but how to remove it?

The only way I can think of is to deflate migrations so that the migration with the function disappears, but this is not always an option.

+4
4

:

, upload_to limit_choices_to, , use_in_migrations = True, , , , . , .

.....

, - , , .

default , upload_to. , :

  • - -
  • . .

:

2 " " , . - . .

+4

, . get_deadline, , . :

  • , get_deadline .
  • , get_deadline.
+2

, get_deadline?

, (.. "" ) . :

  • , . N.

  • , .

  • N + M (.. ) .

, , . N, . N + M, .

0

My direct answer will delete sqlite3.dband delete all migrations except __init__.pyin the migration folder in each application.

Then run python again

$manage.py makemigrations
$python manage.py migrate
-2
source

Source: https://habr.com/ru/post/1662291/


All Articles