Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Django user model between two apps in the same project

I see some similar questions, but they don't appear to be the same or have answers.

I am practicing with Django and trying to make a simple dutch auction project. Initially I thought that the idea would be to create two distinct apps, a buyer app and a seller app, and just have them share databases (or three apps, a commonApp a buyerApp and a sellerApp). However, the more I dig into this the more complicated it seems - I feel like Django isn't really meant to have different apps that are designed around sharing all of their data from one set of tables (maybe I'm wrong?), loosely based on what I've found about having to modify the way Migrations work to accommodate this.

So idea #2, just make one app that separates out the functionality by carefully managing the views, but keeping just one set of models since pretty much all of the data I can think of (the users, the products, etc.) are shared anyway. This seems like it has the advantage of letting Django do all of the data management without my having to sweat about the database design. However, I worry that maybe managing the views will get to be overly complicated.

Maybe there is an idea #3 that makes sense for this sort of project, one that I haven't considered because I am a newb, maybe one that tells me that Django isn't even the right tool for this job...

I tried programming idea #1 and it quickly became spaghetti and only worked when things were very small. I am currently working on idea #2 and so far I think it's going OK, but I'm having trouble conceptualizing how to separate stuff in views, but this could very well just be my lack of experience.

So my question is: is there an obvious resource for this sort of information that I'm missing? If so, could you please point me that way?

like image 317
TrivialCase Avatar asked Nov 29 '25 06:11

TrivialCase


1 Answers

Inside your Django project:

manage.py startapp sellers
manage.py startapp buyers
manage.py startapp common

Add these three apps to settings.py. Depending on your Django's version it can be just 'sellers', 'buyers', 'common' or 'seller.apps.SellerConfig' and so on.

Write your models in common/models.py, and any other logic related to both apps.

Then, in your seller or buyers views:

from common.models import * # or a particular model

Hope this helps.

like image 71
alfonso.kim Avatar answered Nov 30 '25 22:11

alfonso.kim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!