Reputation: 333
I would like to know how to create django relation model for user, folder, file
An user can have multiple files and folders.
A folder can have a lot of files but cant have folder int folder.
I tried something like that( I started learning django one day ago).
I read some docs about ForeignKey and ManytoMany relation from djangobook but i'm not sure if I understand all of it.
class User_t(models.Model):
username = models.CharField(max_length=30)
user_id = models.CharField(max_length=30)
pcw = models.CharField(max_length=30)
name = models.CharField(max_length=30)
surname = models.CharField(max_length=30)
mail = models.EmailField(max_length=50)
validateMail = models.BooleanField()
birthday = models.DateTimeField(max_length=60)
premium = models.BooleanField()
premiumEnd = models.DateTimeField(max_length=10)
totalUpload = models.CharField(max_length=15)
avatar = models.URLField()
class FileItem(models.Model):
file_id = models.CharField(max_length=30)
file_name = models.CharField(max_length=75)
date_upload = models.DateTimeField(max_length=10)
data_size = models.CharField(max_length=75)
key = models.CharField(max_length=75)
owner_id = models.OneToManyField(User_t)
login_accept = models.ManyToManyField(User_t)
file_i = models.FileField(/mnt/test/)
class FolderItem(models.Model):
folder_id = models.CharField(max_length=30)
folder_name = models.CharField(max_length=75)
data_size = models.CharField(max_length=75)
key = models.CharField(max_length=75)
owner_id = models.ForeignKey(User_t)
login_accept = models.ManyToManyField(User_t)
files = models.ForeignKey(FileItem)
Upvotes: 3
Views: 1478
Reputation: 27861
For this sort of thing, you need to use one-to-many relationships. So the idea is that user can have many folders, and each folder can have many files inside of it. From your sample code, you misunderstand how to code these types of relationships. Before I explain that, lets take a step back and figure out how all of this ideally should be stored in db.
You will have to have three tables - users, folders, and files. Each table will have many rows inside and what makes each row unique is this thing we call a primary key. Usually that is an integer. So each user, folder, and file will have their own unique primary key within their own table.
In order to include a file into a folder, each file will have to store to which folder it belongs to. This is where foreign keys are used. Within the file table, there will be column folder
which will specify to which folder that file belongs to by using a foreign key. For example, consider the following row:
id | folder | name | ...
---+--------+------+----
1 | 5 | foo | ...
This tells you that the file belongs to a folder with primary key 5
.
So in one-to-many relations, the child table has to specify to which table it belongs to and not the parent table/model as you have in your example code.
The following is a fix of your code (except I don't know what is logic_accept
is for):
class User_t(models.Model):
username = models.CharField(max_length=30)
user_id = models.CharField(max_length=30)
pcw = models.CharField(max_length=30)
name = models.CharField(max_length=30)
surname = models.CharField(max_length=30)
mail = models.EmailField(max_length=50)
validateMail = models.BooleanField()
birthday = models.DateTimeField(max_length=60)
premium = models.BooleanField()
premiumEnd = models.DateTimeField(max_length=10)
totalUpload = models.CharField(max_length=15)
avatar = models.URLField()
class FileItem(models.Model):
file_id = models.CharField(max_length=30)
file_name = models.CharField(max_length=75)
date_upload = models.DateTimeField(max_length=10)
data_size = models.CharField(max_length=75)
key = models.CharField(max_length=75)
# Django will automatically create owner_id field
owner = models.ForeignKey(User_t, related_name='files')
folder = models.ForeignKey('FolderItem', related_name='files')
# not sure what this (login_accept) is for...
# login_accept = models.ManyToManyField(User_t)
file_i = models.FileField(/mnt/test/)
class FolderItem(models.Model):
folder_id = models.CharField(max_length=30)
folder_name = models.CharField(max_length=75)
data_size = models.CharField(max_length=75)
key = models.CharField(max_length=75)
owner = models.ForeignKey(User_t, related_name='folders')
# again, not sure what this is for
# login_accept = models.ManyToManyField(User_t)
Upvotes: 1