termino_cap_BD

jp.av.dev 2020-11-12 22:25:34 -03:00
parent 2fa7b46cf6
commit 99e168f1c8

@ -33,7 +33,9 @@
| [Envíos] [Promos] | | [Envíos] [Promos] |
|__________________________________| |__________________________________|
``` ```
-------- --------
**Recordar activar entorno virtual** **Recordar activar entorno virtual**
*source /home/sat/weblocal/.django-env/bin/activate *source /home/sat/weblocal/.django-env/bin/activate
@ -54,6 +56,8 @@ Creación primera app : ` python3 manage.py startapp gestionPedidos`
- views.py - views.py
``` ```
### SQLite3 (default)
*/gestionPedidos/models.py* */gestionPedidos/models.py*
``` ```
from django.db import models from django.db import models
@ -170,7 +174,7 @@ postgres@sat:~$ psql
postgres=# \q postgres=# \q
# Crear usuario *ambiente de desarrollo # Crear usuario *ambiente de desarrollo
postgres@sat:~$ createuser --interactive postgres@sat:~$ createuser -P --interactive
Enter name of role to add: django Enter name of role to add: django
Shall the new role be a superuser? (y/n) y Shall the new role be a superuser? (y/n) y
@ -186,8 +190,162 @@ Type "help" for help.
postgres=# \password django postgres=# \password django
postgres=# \q
# Borrar usuario postgres@ratsat:~$ psql -U django -W
```
### Problemas con Log-in ?
```
sudo vim /etc/postgresql/11/main/pg_hba.conf
# CAMBIAR
# Database administrative login by Unix domain socket
local all postgres peer
por
# Database administrative login by Unix domain socket
local all postgres md5
# "local" is for Unix domain socket connections only
local all all md5
# Reiniciar servicio
sudo service postgresql restart
# Para borrar usuario
DROP OWNED BY your_user; DROP OWNED BY your_user;
DROP USER your_user; DROP USER your_user;
```
# Modificar
vim /etc/postgresql/10/main/postgresql.conf
...
listen_addresses = 'localhost,your-server-ip'
...
sudo vim /etc/postgresql/11/main/postgresql.conf
# IPv4 local connections:
host all all 192.168.0.0/24 md5
# Regla Firewall
sudo ufw allow proto tcp from 192.168.0.0/24 to any port 5432
```
Usar algun DBbrowser y crear bd ArticulosClientes
### Instalar Python-Django PostgreSQL connector
```
pip3 install psycopg2
vim settings.py
# Modificar
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
ej.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'articulosclientes',
'USER': 'nombre_usuario',
'PASSWORD': 'clave_usuario',
'HOST': '127.0.0.1',
'DATABASE_PORT': '5432',
}
}
# Check
python3 manage.py check
# Hacer migraciones
python3 manage.py makemigrations
# Migrar
python3 manage.py migrate
```
### Crear un registro
```
python3 manage.py shell
(InteractiveConsole)
>>> from gestionPedidos.models import Clientes
>>>
>>> cli = Clientes(nombre='Pedro', direccion='dnd vive', email='pe@mail.ru', fono=123456789)
>>> cli.save()
>>>
```
Valores en tabla articulos
|id|nombre|seccion|precio|
|--|------|-------|------|
|1|mesa|deco|90|
|2|lámpara|deco|50|
|3|pantalón|vestuario|45|
|4|destornillador|ferreteria|5|
|5|balón|deporte|25|
|6|raqueta|deporte|105|
|7|muñeca|juguetes|15|
|8|tren eléctrico|juguetes|50|
### Intruccion SQL desde Django
```
# Select * ... where seccion='deporte';
>>> from gestionPedidos.models import Articulos
>>>
>>> Articulos.objects.filter(seccion='deporte')
<QuerySet [<Articulos: Articulos object (5)>, <Articulos: Articulos object (6)>]>
>>>
```
*/TiendaOnline/gestionPedidos/models.py*
```
class Articulos(models.Model):
nombre = models.CharField(max_length=30)
seccion = models.CharField(max_length=20)
precio = models.IntegerField()
def __str__(self):
return 'Nombre: %s, Depto. %s, Precio $ %s' % (self.nombre, self.seccion, self.precio)
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py shell
>>> from gestionPedidos.models import Articulos
>>>
>>> Articulos.objects.filter(seccion='deporte')
>>> <QuerySet [<Articulos: Nombre: balón, Depto. deporte, Precio $ 25>,
<Articulos: Nombre: raqueta, Depto. deporte, Precio $ 105>]>
>>>
# Select * ... where nombre='mesa' seccion='deco';
>>> Articulos.objects.filter(nombre='mesa', seccion='deco')
<QuerySet [<Articulo: Nombre: mesa, Depto. deco, Precio $ 90>]>
>>>
# Para buscar por ej. un elemento con precio mayor a 100 desde la shell django
>>> Articulos.objects.filter(precio__gte=100)
<QuerySet [<Articulos: Articulo: raqueta, Depto. deporte, Precio $ 105>]>
otros: __tle, __range, ...).order_by(precio o -precio para inverso)
>>> Articulos.objects.filter(precio__range=(40,90))
>>>
<QuerySet [<Articulos: Nombre: mesa, Depto. deco, Precio $ 90>, <Articulos: Nombre: lámpara, Depto. deco, Precio $ 50>, <Articulos: Nombre: pantalón, Depto. vestuario, Precio $ 45>, <Articulos: Nombre: tren eléctrico, Depto. juguetes, Precio $ 50>]>
>>>
>>> Articulos.objects.filter(precio__gte=50).order_by('precio')
<QuerySet [<Articulos: Articulo: lámpara, Depto. deco, Precio $ 50>, <Articulos: Articulo: tren eléctrico, Depto. juguetes, Precio $ 50>, <Articulos: Articulo: mesa, Depto. deco, Precio $ 90>, <Articulos: Articulo: raqueta, Depto. deporte, Precio $ 105>]>
```
--------