S
S
Salavat Sharapov2015-07-05 16:19:54
Django
Salavat Sharapov, 2015-07-05 16:19:54

Why are imports not visible?

There are a bunch of applications.
In one of the applications in models.py:

# -*- coding: utf-8 -*-
import os
from django.conf import settings

.....
#Имеется вот такой класс для загрузки изображения

#BaseModel - абстракт (описывает только привязку к юзеру)
class Image(BaseModel): 
    def file_uploads_to(instance, filename):
        #from django.conf import settings
        #import os
        file_name = '/'.join(['dir', instance.user.id, 'image', filename])
        file_folder = os.path.join(settings.MEDIA_ROOT, 'dir/%s/image/' % instance.user.id,)
        if not os.path.exists(file_folder):
            os.makedirs(file_folder)
        return file_name

    text = models.TextField('_(Info)', max_length=1000, blank=False, null=True, default='')
    image = models.ImageField('_(Image)', upload_to=file_uploads_to, blank=True)

    class Meta:
                     .....

Until you forcefully import settings and os in the file_uploads_to method, it says that they are NoneType.
In other models of other applications, everything works as expected. What could be causing this behaviour?
The models are all similar in structure, there are no decors and overrides anywhere. Simple.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-07-06
@sim3x

from django.conf.settings import MEDIA_ROOT


......
        file_name = os.path.join('dir', instance.user.id, 'image', filename)
        file_folder = os.path.join(MEDIA_ROOT, 'dir', instance.user.id, 'image')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question