Marek H.
Marek H.

Reputation: 59

Django model for users with own data

I'm going to build an app, where each user has access to only own data. Each user should be able to add/edit/delete own data. Columns in table should looks like this:

"Product name" "Date of purchase" "Price" "Warranty" "Comments"

I assume size of app for up to 100 users and up to 1000 items per user. Now, I'm wondering what kind of model should I build. Should I create one table and add "User" column, and later filter this table by User, or should I create 3 tables: "Users", "Items" and "Users to items relations"? What is your opinion? Which solution will be the best for such app?

Upvotes: 1

Views: 408

Answers (2)

Matt
Matt

Reputation: 10312

One way would be to have 2 models, Users and Items with a ForeignKey to the User model. This will result in 2 tables. Then you can add the logic to only allow a user to modify/delete Item objects that belong to them.

Another option is to use something like django-guardian.

Upvotes: 0

Greg
Greg

Reputation: 1991

You could create 2 tables in one models.py file. One table should have user data, the other one should contain item data, with a foreign key field relating it to a single user.

Upvotes: 1

Related Questions