4

I hope this is not a opinion based question but more like a solutions to a complicated django website. fyi, i am quite a beginner in django.

first of all, i am doing a complicated accounting django website where there are features like:

  • purchases (purchase order, quotation request, quotation, invoice, do)

  • Custom user roles and permissions, because we want user to setup their own roles and permissions system

  • sales (POS, stocks, too many to mentions)

  • user and registrations

  • incomes & expenses tracking and reporting.

There are articles and SO's answers about 'many apps vs 1 large app'. My confusion started. I figured out django allows seperating views.py and models.py into multiple file in app/views and app/models with init.py imports.

I personally do not like large app file as it is hard to locate things. I prefer neat structures. But the confusion keeps attacking. I want to do one thing and do it well but it seem like having one large app makes more sense because all the mentioned features are ForeignKey dependent.

So, according to your experience. what's your ideal folder structures and solutions to deal with this?

If you could provide performance difference that would be helpful.

UPDATE: Since most people said multi apps, I have last question regarding this. Since Django app can have models/ & views/ folder with multiple models.py & views.py inside, that means one large app can be seperated into multiple views files inside a views/ folder. What yall think about this? since this will put all migrations in one place, does it provide long term safety in term of messy things like foreign keys across apps.

Loonb
  • 1,068
  • 5
  • 18
  • 30
  • 1
    many apps. personally. – tim Mar 08 '19 at 03:44
  • 1
    If you think you'll ever need to reuse any of these features in another project, keep it in a separate app. The way I do it is that I keep closely related features in a single app. For example, these - *"quotation request, quotation, invoice"* - seem closely related. They can be used in multiple projects, like in e-commerce, in a freelancer's website, in an agency site, etc. So I'd keep these in a single app. – xyres Mar 08 '19 at 05:13
  • @xyres after digging so much information, i decide to follow your steps. I move closely related (very interdependent) apps into single app while spitting the views.py and models.py into submodule. Oh ya please check the updated question, if you have ideas or feedback please comment thanks in advance. – Loonb Apr 02 '19 at 15:54

3 Answers3

3

This kind of question I have asked long time back, on different platform. Common answer is

If you want to reuse that, then create an app or if you don't want then you no need to make a separate app

Let me give you an example. If your project has features like

  • Sharing an Image
  • Sharing text
  • Comment on Media and Text
  • Upvote and Downvote options

Here an user can share either a image or text and other can comment on it or Upvote/Downvote it. In this case If you make Comment a separate app and Upvote/Downvote a separate app, then in future if you have to add Video along with Image and Text, then all you have to link Video to Comment app and Upvote/DOwnvote app, that will be less task comparing to a big app with all things inside. Also You can manage your database accordingly.

In your case, you can make (for example),

  • Custom User (help you add extra permission in future)
  • Product Category (help you to add more category or subcategory)
  • Products or Sales etc.

For many admins, when you add those apps to your settings.py, and if you edit admin.py separately in each app, everything will appear your admin panel without any hassle.

For many views, you can import all the models to views.py of any app or use it separately in their apps. It will not cause any problem. Same for urls.py and forms.py etc.

Bidhan Majhi
  • 1,320
  • 1
  • 12
  • 25
  • Thanks for the quick response. is there any performance issue when I have let's say 50 apps in a project? let's say each app handle very detail task. – Loonb Mar 08 '19 at 08:54
  • 1
    Performance depends on other factors than number of apps such as Caching, Database architecture etc. You need to know what performance improvement you want. Let's say you can add AJAX and Cache data and do a lot of task in a single page with a good speed. So I don't think number of apps will be an issue. – Bidhan Majhi Mar 08 '19 at 09:59
  • Have you used this single app structure? What are other disadvantages in multiple comparing to single app. – l.b.vasoya Apr 13 '22 at 06:41
1

For your solution, i will recommend that you create multiple apps, with each app containing it's own view.py, models.py and urls.py. What you can do is, create a separate app for user that will contain User model (if you decide to override the existing user model provided by django) and all user related views (such as login, registration etc.). I will recommend that you create a separate model for Role as well that will contain all roles in your system. Make a management command that will add roles in that table, whenever you decide to add a new role. Create an enum type class like:

class Role(enum.Enum):
    ADMIN = 1
    USER= 2

    labels = {
       ADMIN: "Admin",
       USER: "User"
     }

So the first entry in Role table will be Admin with pk=1 and so on.

This was an idea as to how you can create models and keep things separate. You can associate user from a separate app to a table in your sales app as that won't cause you any problem and keep the code readable and things neat.

Bolshoi Booze
  • 466
  • 8
  • 22
1

For your own sanity go for multiple apps.

just lay them out first and figure out what should logically go together in one app and what should go to the next.

You want to avoid running into circular imports!

Foreign keys across apps is no problem at all. Basically as soon as u use the USER anywhere in your models, you'll reference across app boundaries already.

imolitor
  • 780
  • 6
  • 12