0

I'm building a Django app with Django 1.8 where users can signup, but they may upload many files while using my website.

This is a problem because it seems that in Django a FileField is a database column, and I can't just add a column each time a user wants to upload a new file.

Also the files may be totally different and not follow a fixed system, so I can't have a column that says passport or lease, I want to wrap the FileField with a description, and store that in the database somewhere. How is that done with best practices in Django?

1
  • There are various possible approaches but one of them is to create an "Attachment" model that has a FileField, a description field, and a ForeignKey to your main model. Commented May 14, 2016 at 9:39

1 Answer 1

1

This problem does not apply solely to files, but to any case where you want the user to add an arbitrary number of any piece of data. And the solution is the same: define a separate table with a ForeignKey to the user.

class UserUpload(models.Model):
    file = models.FileField(...)
    description = models.CharField(...)
    user = models.ForeignKey(User)

Now you can get all a user's uploads with user.userupload_set.all().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.