Skip to content

Instantly share code, notes, and snippets.

View anshuUnity's full-sized avatar
🎯
Learning

Anshu Pal anshuUnity

🎯
Learning
View GitHub Profile
@anshuUnity
anshuUnity / Custom_user.py
Created August 9, 2021 17:42
Django custom user model
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have a username')
def index(request):
products = Product.objects.all()
if request.method == "POST":
Check = request.POST.getlist('checking')
quan = request.POST.getlist('quan')
# concurrent list
newQuan = []
from django.shortcuts import render
from django.views.generic import View
from upload.forms import UploadForm
from django.http import JsonResponse
# Create your views here.
class HomeView(View):
def get(self, request):
form = UploadForm()
from django import forms
from upload.models import Upload
class UploadForm(forms.ModelForm):
class Meta:
model = Upload
fields = ['title', 'image']
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
class Upload(models.Model):
title = models.CharField(max_length=50)
image = models.ImageField()
def __str__(self):
return self.title
@anshuUnity
anshuUnity / m2m.py
Created July 26, 2021 17:44
m2m test
@receiver(m2m_changed, sender=Order.order_name.through)
def checkquan(sender, instance, **kwargs):
print("Working")
prod = instance.order_name.all()
for p in prod:
p.quantity -= instance.order_quantity
p.save()
from django.core.management.base import BaseCommand
from django.utils import timezone
class Mycommand(BaseCommand):
help = 'Test if custom command is working or not'
def handle(self, *args, **kwargs):
self.stdout.write("Hi all set, it is working.")
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
...............
<a href="{% url 'users_csv' %}">Export CSV</a>
...............