· 

Django: class ClientDocument(models.Model)

1. Introduction

This class uploads a document to the server and saves it somewhere in the server or the database.  What is model.py like?

 

2. file = models.FileField(upload_to="client_documents/")

Can you explain this?

class ClientDocument(models.Model):

    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    file = models.FileField(upload_to="client_documents/")

    uploaded_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):

        return f"{self.file.name} ({self.owner.username})"

 

Sure — let’s break down your ClientDocument model line by line.

class ClientDocument(models.Model):
  • This defines a new Django model named ClientDocument.

  • It inherits from models.Model, which means Django will create a database table for it.


owner = models.ForeignKey(User, on_delete=models.CASCADE)
  • owner: A foreign key linking this document to a specific User.

  • ForeignKey means there is a many-to-one relationship: many ClientDocuments can belong to one User.

  • on_delete=models.CASCADE means that if the linked User is deleted, all of their ClientDocument records will also be deleted.


file = models.FileField(upload_to="client_documents/")
  • file: Stores the uploaded file.

  • FileField automatically handles file storage.

  • upload_to="client_documents/" means that uploaded files will be stored in the MEDIA_ROOT/client_documents/ folder.


uploaded_at = models.DateTimeField(auto_now_add=True)
  • uploaded_at: Stores the date and time when the file was first uploaded.

  • auto_now_add=True automatically sets this field to the current date and time when the object is first created (it won’t change afterwards).


def __str__(self):
return f"{self.file.name} ({self.owner.username})"
  • Defines how this model will be represented as a string (e.g., in the Django admin or in the shell).
  • It will display something like:

    client_documents/resume.pdf (john_doe)

    where resume.pdf is the file name and john_doe is the username of the owner.


💡 In the database, this will typically create a table with columns like:

  • id (primary key, auto-added by Django)

  • owner_id (foreign key to the auth_user table)

  • file (stores the path to the uploaded file)

  • uploaded_at (timestamp)

 

If you want, I can also show you the exact SQL table structure Django would create for this model. Would you like me to?

 

3. admin site

You said:

def __str__(self): 

    return f"{self.file.name} ({self.owner.username})"

Defines how this model will be represented as a string (e.g., in the Django admin or in the shell).

On the other hand, I see there is a pull-down list of usernames in the admin site's "add client document" section. Is this the effect of the above two lines of code?

 

Not exactly — the pull-down list of usernames in the admin does not come from the __str__ method of ClientDocument.

Here’s what’s happening:

  • The pull-down list is generated because of the ForeignKey field:

    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    Django knows owner is related to the User model, so in the admin form it automatically provides a dropdown of all existing users.

  • The __str__ method affects how instances of ClientDocument are displayed in lists or dropdowns when you are referencing this model itself, not the User.

Example:

  1. In the admin list of ClientDocument entries, you’ll see something like:

    client_documents/resume.pdf (john_doe)

    That’s the __str__ output.

  2. In the admin form for adding a new ClientDocument, the owner field shows a dropdown of usernames. That’s automatic because owner is a ForeignKey to User. Django uses User.__str__(), which returns the username by default, to label the options in the dropdown.

✅ So the pull-down list comes from the User model, while ClientDocument.__str__() controls how your documents themselves are displayed in the admin list.

 

If you want, I can draw a diagram showing the dropdown vs. list display to make it super clear. Do you want me to?