Reputation: 1545
Note: I read Choosing between Stack Overflow and Software Engineering and from my conclusion since this post included code I figured it belonged on stackoverflow instead of programmers.
Hi, I'm building an application in Django and I'm very new to Django. I've read the Django Tutorial, and the Django-Rest-Framework tutorial.
My question is how should I structure this part of my application. Right now I'm trying to build a RESTful api for an android application I am writing.
I've read this:
Why does django enforce all model classes to be in models.py?
and
models.py getting huge, what is the best way to break it up?
To put my project into perspective:
Here's what my models.py looks like: (Note these are not my actual models they are just an example of what I'm doing to keep the code more concise)
class Company(models.Model):
name = models.charField(max_length=100)
class Department(models.Model):
company = models.ForeignKey(Company)
employees_count = models.IntegerField(default=1)
name = models.charField(max_length=100)
class Employee(models.Model):
department = models.ForeignKey(Department)
name = models.charField(max_length=100)
age = models.IntegerField(default=0)
Each respective class would have their own functions would be accessible through an API.
My question after an introduction to my project is.. Would the Company, Department, and Employee all belong in different packages since they are separate entities? Or should they all belong in one models file? In the end they should have their own REST class as described here: http://www.django-rest-framework.org/tutorial/3-class-based-views#rewriting-our-api-using-class-based-views
Upvotes: 2
Views: 1740
Reputation: 2882
Django is fine with both approaches. In Python, unlike other languages, you can place multiple class definitions in the same file. Typically, a Django app will have all its models in the same file if it doesn't get too long say 2-3 screens.
Anything longer and they are typically placed inside a models
module and split into different files. Each model needs to be mentioned in the __init__.py
as mentioned in your first link. In Django 1.7, the last app_label
"trick" mentioned there is not required any more.
Upvotes: 1