Skip to content

Commit d250278

Browse files
committed
Add project
0 parents  commit d250278

21 files changed

+748
-0
lines changed

.gitignore

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
113+
# Spyder project settings
114+
.spyderproject
115+
.spyproject
116+
117+
# Rope project settings
118+
.ropeproject
119+
120+
# mkdocs documentation
121+
/site
122+
123+
# mypy
124+
.mypy_cache/
125+
.dmypy.json
126+
dmypy.json
127+
128+
# Pyre type checker
129+
.pyre/
130+
131+
.DS_Store
132+
133+
media/
134+
staticfiles/
135+
.idea
136+
.ipynb_checkpoints/
137+
.vscode
138+
*.cast

README.md

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# NocoDB Django Example
2+
3+
[NocoDB](https://github.com/nocodb/nocodb)
4+
5+
**Doc:** https://nocodb.com/
6+
7+
![](https://private-user-images.githubusercontent.com/86527202/277104231-e2fad786-f211-4dcb-9bd3-aaece83a6783.gif?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgwNDExNjQsIm5iZiI6MTcxODA0MDg2NCwicGF0aCI6Ii84NjUyNzIwMi8yNzcxMDQyMzEtZTJmYWQ3ODYtZjIxMS00ZGNiLTliZDMtYWFlY2U4M2E2NzgzLmdpZj9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjEwVDE3MzQyNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWRkNDlkMTZiNWU4NTA4NzBhNjg0OGY1NTZmODUzZTM4NGY2ZTRkNzZkMzE5NzhjMzljZmRhZWYwYWNlYmJhOTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.rxO0_tC5zmy1Ax4cZ6c_2WVy1-4ERCJ_pGexW7iRb6c)
8+
9+
10+
# Install
11+
12+
## docker-compose
13+
14+
https://github.com/nocodb/nocodb/blob/develop/docker-compose/pg/docker-compose.yml
15+
16+
```
17+
docker-compose up -d
18+
```
19+
20+
Enter in http://0.0.0.0:8080/dashboard/#/signup
21+
22+
Create tables on NocoDB.
23+
24+
25+
26+
## Django step by step
27+
28+
29+
Create virtualenv.
30+
31+
```
32+
python -m venv .venv
33+
source .venv/bin/activate
34+
```
35+
36+
Gera .env
37+
38+
```
39+
python contrib/env_gen.py
40+
```
41+
42+
Instala as libs.
43+
44+
```
45+
pip install -r requirements.txt
46+
```
47+
48+
Create Django project.
49+
50+
```
51+
django-admin startproject backend .
52+
```
53+
54+
Create apps.
55+
56+
```
57+
cd backend
58+
python ../manage.py startapp core
59+
python ../manage.py startapp product
60+
```
61+
62+
63+
Edit `settings.py`
64+
65+
```python
66+
# settings.py
67+
from pathlib import Path
68+
69+
from decouple import config, Csv
70+
71+
72+
BASE_DIR = Path(__file__).resolve().parent.parent
73+
74+
SECRET_KEY = config('SECRET_KEY')
75+
76+
DEBUG = config('DEBUG', default=False, cast=bool)
77+
78+
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv())
79+
80+
81+
INSTALLED_APPS = [
82+
'django.contrib.admin',
83+
'django.contrib.auth',
84+
'django.contrib.contenttypes',
85+
'django.contrib.sessions',
86+
'django.contrib.messages',
87+
'django.contrib.staticfiles',
88+
# others apps
89+
'django_extensions',
90+
# my apps
91+
'backend.core',
92+
'backend.product',
93+
]
94+
95+
DATABASES = {
96+
'default': {
97+
'ENGINE': 'django.db.backends.postgresql',
98+
'NAME': config('POSTGRES_DB', 'root_db'),
99+
'USER': config('POSTGRES_USER', 'postgres'),
100+
'PASSWORD': config('POSTGRES_PASSWORD', 'password'),
101+
'HOST': config('DB_HOST', 'localhost'),
102+
'PORT': config('DB_PORT', 5431, cast=int),
103+
}
104+
}
105+
106+
LANGUAGE_CODE = 'pt-br'
107+
108+
TIME_ZONE = 'America/Sao_Paulo'
109+
110+
STATIC_URL = 'static/'
111+
STATIC_ROOT = BASE_DIR.joinpath('staticfiles')
112+
```
113+
114+
Edit `core/apps.py`
115+
116+
```python
117+
# core/apps.py
118+
from django.apps import AppConfig
119+
120+
121+
class CoreConfig(AppConfig):
122+
default_auto_field = 'django.db.models.BigAutoField'
123+
name = 'backend.core'
124+
```
125+
126+
Edit `product/apps.py`
127+
128+
```python
129+
# product/apps.py
130+
from django.apps import AppConfig
131+
132+
133+
class ProductConfig(AppConfig):
134+
default_auto_field = 'django.db.models.BigAutoField'
135+
name = 'backend.product'
136+
```
137+
138+
### Inspectdb
139+
140+
Run Inspectdb.
141+
142+
```
143+
python manage.py inspectdb
144+
```
145+
146+
Edit `product/models.py`
147+
148+
```python
149+
# product/models.py
150+
from django.db import models
151+
152+
from backend.core.models import TimeStampedModel, CreatedBy
153+
154+
155+
class Product(TimeStampedModel, CreatedBy):
156+
title = models.CharField(max_length=100, blank=True, null=True)
157+
category = models.ForeignKey(
158+
'Category',
159+
on_delete=models.CASCADE,
160+
related_name='products',
161+
db_column='nc_t_52___Category_id',
162+
null=True,
163+
blank=True
164+
)
165+
166+
class Meta:
167+
managed = False
168+
db_table = 'nc_t_52__Product'
169+
170+
def __str__(self):
171+
return f'{self.title}'
172+
173+
174+
class Category(TimeStampedModel, CreatedBy):
175+
title = models.CharField(max_length=100, blank=True, null=True)
176+
177+
class Meta:
178+
managed = False
179+
db_table = 'nc_t_52___Category'
180+
181+
def __str__(self):
182+
return f'{self.title}'
183+
```
184+
185+
Edit `product/admin.py`
186+
187+
```python
188+
# product/admin.py
189+
from django.contrib import admin
190+
191+
from .models import Product, Category
192+
193+
194+
@admin.register(Product)
195+
class ProductAdmin(admin.ModelAdmin):
196+
list_display = ('__str__',)
197+
search_fields = ('title',)
198+
list_filter = ('category',)
199+
200+
201+
admin.site.register(Category)
202+
```
203+
204+
Run
205+
206+
```
207+
python manage.py makemigrations
208+
python manage.py migrate
209+
python manage.py runserver
210+
```
211+
212+
Open
213+
214+
http://localhost:8000/admin/

backend/__init__.py

Whitespace-only changes.

backend/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for backend project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
15+
16+
application = get_asgi_application()

backend/core/__init__.py

Whitespace-only changes.

backend/core/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CoreConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'backend.core'

backend/core/migrations/__init__.py

Whitespace-only changes.

backend/core/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.db import models
2+
3+
4+
class TimeStampedModel(models.Model):
5+
created_at = models.DateTimeField(blank=True, null=True)
6+
updated_at = models.DateTimeField(blank=True, null=True)
7+
8+
class Meta:
9+
abstract = True
10+
11+
12+
class CreatedBy(models.Model):
13+
created_by = models.CharField(max_length=100, blank=True, null=True)
14+
updated_by = models.CharField(max_length=100, blank=True, null=True)
15+
16+
class Meta:
17+
abstract = True

backend/product/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)