3

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
rs_punia
  • 421
  • 2
  • 6
  • 17
2v Cheng
  • 31
  • 3
  • 2
    Django raises this error because it cannot reconcile the missing function with the migration history. You should find the previous migration files where it is named, and delete the function name where it says upload_to. Then try your migration again. I hope this helps! – Milo Persic Mar 02 '22 at 04:23

1 Answers1

3

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.

  • thanks! when I set `bar` argument `baz` into `None` in **migration file** and `makemigrations`, it works! but I have to modify **migration file** manually locally and on remote server when deploy. – 2v Cheng Mar 02 '22 at 05:23