Home > Mobile >  Django FileNotFoundError when trying to add an item to a model
Django FileNotFoundError when trying to add an item to a model

Time:11-17

I am trying to create some models using Django and python. All my models are working and I can add items to the models, except for my User model. Can anyone suggest what could be wrong about it?

models.py

from django.db import models

# Create your models here.

class Item(models.Model):
    ID = models.IntegerField(auto_created=True, primary_key=True, unique=True)
    Name = models.CharField(max_length=30)
    Price = models.IntegerField()


class User(models.Model):
    ID = models.IntegerField(auto_created=True, primary_key=True)
    Nickname = models.CharField(max_length=50)
    Password = models.CharField(max_length=200)
    Mail = models.EmailField()
    Points = models.IntegerField()
    SaleItem = models.ForeignKey(Item, on_delete=models.CASCADE)
    ProfilePic = models.FilePathField()
    BannerPic = models.FilePathField()
    ZodiacSign = models.CharField(max_length=50)


class Friendship(models.Model):
    ID = models.IntegerField(auto_created=True, primary_key=True, unique=True)
    UserID = models.ManyToManyField(User, related_name="user")
    FriendID = models.ManyToManyField(User, related_name="befriended_user")


class ThreadPrivate(models.Model):
    ID = models.IntegerField(auto_created=True, primary_key=True, unique=True)
    Text = models.TextField()
    Sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Sender")
    Receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Receiver")
    Time = models.DateTimeField(auto_now_add=True)


class Side(models.Model):
    ID = models.IntegerField(auto_created=True, primary_key=True, unique=True)
    Creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name="creator_of_page")
    Time = models.DateTimeField(auto_now_add=True)


class ThreadPublic(models.Model):
    ID = models.IntegerField(auto_created=True, primary_key=True, unique=True)
    Text = models.TextField()
    Time = models.DateTimeField(auto_now_add=True)
    Sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="thread_sender")
    SideID = models.ForeignKey(Side, on_delete=models.CASCADE, related_name="side_for_thread")

and admin.py

from django.contrib import admin
from .models import *
# Register your models here.

admin.site.register(Item)
admin.site.register(User)
admin.site.register(Friendship)
admin.site.register(ThreadPrivate)
admin.site.register(Side)
admin.site.register(ThreadPublic)

I have remembered to add to APPS in settings.py and such. All models work, except for the User model. Any ideas?

picture of the error: enter image description here

CodePudding user response:

You have to provide a path to the FilePathField of the user model. According to this answer you could create the variable FILE_PATH_FIELD_DIRECTORY in your settings and then use it like:

from django.conf import settings

class User(models.Model):
    ProfilePic = models.FilePathField(path=settings.FILE_PATH_FIELD_DIRECTORY)

It would be helpful to change the name of your class to CustomUser, so you don't confuse it with the User class from django.

  • Related