Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:51:43+00:00 2026-06-13T09:51:43+00:00

I would like to know how to create django relation model for user, folder,

  • 0

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.

User model

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()

File model

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/)

Folder model

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:51:44+00:00Added an answer on June 13, 2026 at 9:51 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to create a php function that can be
I would like to know how can I create a regexp to match the
I would like to know a way to create an XML file using MySQL
I would like to know how to create a linked list of linked lists.
i would like to know how to create a hidden type input box and
I would like to know how to create a computed observable array. In my
I would like to know how to create a rounded corners on a table
Hi I would like to know how to create a dll that is written
I'm an amateur web developer and I would like to know how to create
I would like to know if there is a method to create share buttons

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.