0

I am making a site with Django 1.10 (Python 3). The purpose is to track what book I have and which ones i have read.

I want to make simple CRUD pages for a few different models. The different models I have at the moment are: Books, Authors and Publishers. This list will probably grow. I have read about splitting your site into smaller apps. So one app for the books CRUD pages, one for the authors and one for the publishers.

Is this the way intented by Django? If so a question arises. How do we seperate the models? The Books model has fields that depend on Author and Publisher. But since they are in their own app now, how am I supposed to access it? I am not liking the idea of just importing from another app since they are supposed to be seperate apps.

OmerSakar
  • 175
  • 2
  • 11
  • Your last sentence doesn't follow at all. It's fine to create a relationship across apps; indeed you have to for example if you want to connect a model with a user. – Daniel Roseman Dec 17 '16 at 23:32

2 Answers2

0

If they are related then don't separate them. Instead of that, you can re-consider your models' structure and build them again.

I do not know exact fields belong to Author and Publisher but you may combine them in to one model. This is just an idea.

Or you just can create tables like Author, Publisher etc.

There is a process named Normalization which favors, basically, creating tables with small amount of columns instead of creating a big one.

Additionally you may view this thread, too.

Community
  • 1
  • 1
alioguzhan
  • 7,657
  • 10
  • 46
  • 67
0

I want to make simple CRUD pages for a few different models. The different models I have at the moment are: Books, Authors and Publishers. This list will probably grow. I have read about splitting your site into smaller apps. So one app for the books CRUD pages, one for the authors and one for the publishers.

Personally I think a single app can handle the three models easily.

Is this the way intented by Django? If so a question arises. How do we seperate the models? The Books model has fields that depend on Author and Publisher. But since they are in their own app now, how am I supposed to access it? I am not liking the idea of just importing from another app since they are supposed to be seperate apps.

In order to access the fields you need to implement a Foreign key relationship. Check the docs here.

If you still think the models shall live in different apps you will need to import them before using them. Say the Book model needs the Author model. What you will need to do is to import the Author model and link it to the book model with a ManytoMany or ForeignKey relationship.:

from django.db import models
from app_authors.model import Author


class Book()models.Model:
    author = ForeignKey(Author, null=True, blank = True, verbose_name = 'Author')
Bogdan
  • 87
  • 7