Saturday, 16 September 2023

Server

 Python manage.py runserver ===> It is used for Development server, it has good feature auto restart when we are add or edit smoothing development time, as well as servers static files.

Default port server ===> localhost:8000  


But production we required our application more secure needed,

WSGI(Web server gateway interface): WSGi basically interact with python app and web Server.


gunicorn server example of WSGI

nginx example of web server

static code - image, js file, css file

Dynamic code - python code


Gunicorn: Gunicorn, is a Web Server Gateway Interface (WSGI), WSGI server such as gunicorn

1. proper logging feature

2. it fast

3. it has buildin security mesure

4. It is ideally used production or staging enverment in django web app

5. WSGI server such as gunicorn not able to automatically served static files, that's why NGINX web server needs


NGINX is reverse proxy server that accept request from client and respond to the client 







Monday, 4 September 2023

QuerySet

 models.py

from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.exceptions import ValidationError


def validate_restaurant_name_begins_with_a(value):
    if not value.startswith('a'):
        raise ValidationError('Restaurant name must begin with "a"')


class Restaurant(models.Model):
    class TypeChoices(models.TextChoices):
        INDIAN = 'IN', 'Indian'
        CHINESE = 'CH', 'Chinese'
        ITALIAN = 'IT', 'Italian'
        GREEK = 'GR', 'Greek'
        MEXICAN = 'MX', 'Mexican'
        FASTFOOD = 'FF', 'Fast Food'
        OTHER = 'OT', 'Other'


    name = models.CharField(max_length=100, validators=[validate_restaurant_name_begins_with_a])
    website = models.URLField(default='')
    date_opened = models.DateField()
    latitude = models.FloatField(validators=[MinValueValidator(-90), MaxValueValidator(90)])
    longitude = models.FloatField(validators=[MinValueValidator(-180), MaxValueValidator(180)])
    restaurant_type = models.CharField(max_length=2, choices=TypeChoices.choices)

    def __str__(self):
        return self.name


class Rating(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE, related_name='ratings')
    rating = models.PositiveSmallIntegerField(
        validators=[MinValueValidator(1), MaxValueValidator(5)]
    )

    def __str__(self):
        return f"Rating: {self.rating}"
   

class Sale(models.Model):
    restaurant = models.ForeignKey(
        Restaurant, on_delete=models.SET_NULL, null=True, related_name='sales')
    income = models.DecimalField(max_digits=8, decimal_places=2)
    datetime = models.DateTimeField()

views.py

How to access parent model to child model using for loop .py file

Here "ratings" related_name

    restaurants = Restaurant.objects.all()# filter(name__istartswith='a')
    for i in restaurants:
        for j in i.ratings.all():
            print(j.rating)

How to access parent model to child model using for loop .html file

        {% for res in restaurants %}
            <p>{{res.name}}</p>
             <ul>
                {% for rat in res.ratings.all %}
                    <li>{{rat.rating}}</li>
                {% endfor %}
            </ul>
         {% endfor %}

How to access parent and child togather like object in .py file










Server

 Python manage.py runserver ===> It is used for Development server, it has good feature auto restart when we are add or edit smoothing d...