django makemigrations raise error after remove a function in models.py

Viewed 198

I define a model named Foo which has a FileField named bar, and I define a function baz which is callable used for bar's upload_to arguement in same file. When I removed the bar field and baz funcion and perform makemigrations operations, it raise error "AttributeError: module 'models' has no attribute 'baz'".

How can I solve the bug?

below is snippet demo

import os
import time

from django.db import models


def baz(instance, filename):
    ext = filename.split('.')[-1]
    fn = time.strftime('%Y%m%d%H%M%S')
    com = fn + '.' + ext
    return os.path.join('', com)


class Foo(models.Model):
    name = models.CharField(max_length=255)
    bar = models.FileField(upload_to=baz)  # wants to remove
1 Answers

It's because of migration file. To resolve you can change upload path to string in migrations file that contains bar field initial create. Change migrations field upload_to path.

Related