OSError : media upload error

error :

Internal Server Error: /admin/core/profile/aEfUZ89qMeZ2SpWdLPYVJS/change/
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/core/files/storage/filesystem.py", line 109, in _save
    locks.lock(fd, locks.LOCK_EX)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/core/files/locks.py", line 121, in lock
    fcntl.flock(_fd(f), flags)
OSError: [Errno 38] Function not implemented

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/contrib/admin/options.py", line 716, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/utils/decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/utils/decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/views/decorators/cache.py", line 80, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/contrib/admin/sites.py", line 240, in inner
    return view(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/contrib/admin/options.py", line 1948, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/utils/decorators.py", line 48, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/utils/decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/utils/decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/contrib/admin/options.py", line 1804, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/contrib/admin/options.py", line 1855, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/contrib/admin/options.py", line 1259, in save_model
    obj.save()
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/db/models/base.py", line 822, in save
    self.save_base(
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/db/models/base.py", line 909, in save_base
    updated = self._save_table(
              ^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/db/models/base.py", line 1031, in _save_table
    values = [
             ^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/db/models/base.py", line 1035, in <listcomp>
    (getattr(self, f.attname) if raw else f.pre_save(self, False)),
                                          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/db/models/fields/files.py", line 317, in pre_save
    file.save(file.name, file.file, save=False)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/db/models/fields/files.py", line 93, in save
    self.name = self.storage.save(name, content, max_length=self.field.max_length)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/core/files/storage/base.py", line 38, in save
    name = self._save(name, content)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/core/files/storage/filesystem.py", line 116, in _save
    locks.unlock(fd)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/django/core/files/locks.py", line 127, in unlock
    fcntl.flock(_fd(f), fcntl.LOCK_UN)
OSError: [Errno 38] Function not implemented

settings.py :

MEDIA_URL = '/media/' 
MEDIA_ROOT = BASE_DIR / 'media'

models.py :

def profile_path(instance,filename):
    return f'user-{instance.user.username}/profile/{filename}'
class Profile(models.Model):
    uuid = models.CharField(primary_key=True,max_length=22,default=shortuuid.uuid,editable=False)
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    uname = models.CharField(max_length=20)
    profile_pic = models.ImageField(upload_to='profile/')
    def __str__(self):
        return self.uname

That line … it appears you forgot to apply profile_path function object. Like so
profile_pic = models.ImageField(upload_to=profile_path)

That’s not a problem man !!
I can’t save model with that ImageField it throwing error OSError even i tried to save them on admin page !

try:

pip uninstall -y django
pip install django

Otherwise the traceback is missing out some lines that could identify the source of the error

Welcome @shaheen-coder !

Let’s take a step back and cover some basics.

What version of Django are you using?

Are you using any custom storage backends?

Is this your development environment or a production environment?

How are you running your project?

hi @KenWhitesell
im using current version 5.0.7 on linux 4.19 kernel
and i ain’t using any custom backend storage
this is not a production code just a prototype.
that error coming from django/core/files/lock locks.py
i guess its sort of django framework bug

this model migrations and migrate run without any error or caution
and i tried to add data through admin page then i getting this error

Show your code: model, view and admin.py

These items have little (if anything) to do with uploading files. Uploaded files are not stored in the database. Only the file name is stored in the database.

Almost certainly not. Yes, it’s possible. However, that’s going to be the last conclusion I’d come to - once all other reasonable possibilities have been eliminated.

What operating system are you running this on?

How are you running your project?

What uid is running your code?

What are the permissions on the BASE_DIR / 'media' directory?