Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django is it okay to have models of two different apps dependent of each other?

I am working on a billing and accounting application from scratch in which I have created two different apps as:

  1. billing: this app stores all the billing related logic for example; subscription plans, products, coupons and similar stuff.
  2. accounts: this app keeps the accounting data for example: Customer account, transactions etc.

Now I have a issue I need to relate both of them in the following situations:

  1. Plans(In billing app) subscribed by the Customer (In accounts app)
  2. Product(In billing app) purchased by the Customer (In accounts app)
  3. Customer(In accounts app) Invoices (In billing app)
  4. Coupons(In billing app) applied/redeemed by the Customer(In billing app)

I have tried using the foreign keys in the models among these apps but it just looks very crude and bad.

I am not sure wether I should put all the models in the same or so; I would like to have a review on my design by someone insightful so that I can know what are the better ways of doing this.

like image 528
akm Avatar asked Oct 12 '15 07:10

akm


People also ask

How many apps can a Django project have?

Django comes with six built-in apps that we can examine.

What is the point of Django apps?

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.


1 Answers

It's perfectly fine. To avoid import loops you can use string definitions;

module 'foo/models.py'

class Foo(models.Model):
    bar = models.ForeignKey('bar.Bar')  # no explicit import statement required

module 'bar/models.py'

class Bar(models.Model):
    foo = models.ForeignKey('foo.Foo')

The real question is does your structure make things more intuitive? If you're unsure why you need to split depended pieces of code, don't.

Related topic: Separation of business logic and data access in django

like image 75
Hedde van der Heide Avatar answered Nov 01 '22 18:11

Hedde van der Heide