1

Where i must create classes in order to manipulate data of the database? I have 4 app in my project. Can i create an app specially for that, and create all the classes/functions in the models.py ?

QDex
  • 139
  • 9

2 Answers2

3

The better approach is to have each app its own models.py. which would define the database tables each app is using.

And to have each app its own views.py. which would define the logic of the app and manipulation of the data of your database.

I don't think you really understand what apps really are in Django and you can learn about them in here look at other answer and mine too.

Ahtisham
  • 9,170
  • 4
  • 43
  • 57
  • i read it, thx. For the app Car/, where can i create a new class with constructor and new functions to manipulate data of the class Car witch is define in the models.py ? – QDex Feb 02 '19 at 22:12
  • Thank you. In this link (https://stackoverflow.com/questions/19350785/what-s-the-difference-between-a-project-and-an-app-in-django-world/54490497#54490497) i can not tcheck it because it's already tcheked by someone else. – QDex Feb 03 '19 at 16:10
1

yes, it's recommended to create your models in each of your apps directory for reusability.

edit* check out this answer if you're wondering how to implement singleton in django,

Yeonghun
  • 400
  • 5
  • 13
  • Yes thank you, that's what i did. But i'd like to know where to create class ofr functions to pass queries. For exemple, i have two app: Student, Teacher. If i have a query wich join two tables of those apps, in what model that i will create a function for manipulate data? – QDex Feb 01 '19 at 23:08
  • Oh i see, you can checkout [select_related](https://docs.djangoproject.com/en/1.10/ref/models/querysets/#django.db.models.query.QuerySet.select_related) in that case, it's bascically a join query – Yeonghun Feb 01 '19 at 23:22
  • Okey thx; Is it in the models.py that i must create fucntions or othern classes to maque queries ? – QDex Feb 01 '19 at 23:51
  • Your models represent classes and as well as tables of database. – Muhammad Faizan Fareed Feb 02 '19 at 14:21
  • QTcho, functions you can create everywhere you like. Python is very flexible language, and Django is just a python code, although quite big project. There's no restrictions how you can structure your code. So you can create functions in convenient places accordingly to your application structure and `import` them where necessary. One detail is that django has some "common" approaches for queries, like `QuerSet` objects or `Manager` classes. You may probably like to use them too – gvm Feb 04 '19 at 18:20