A django Application
Users
Authetication
search
list
select
cart/wishlist
booking/order
payment
notification
Users
email
mobile
password
otp
Authentication
API
register
login
generate_otp
verify_otp
forgot password
change password
Search
name
location
category
list
name
price
rate
order/booking
Payment
django application
model
serializers
views
urls
Model
class Basemodel(models.Model):
created=datetimstamp
updated=datetimstamp
class TableName(models.Model,User,Basemodel):
field_name=Type
name=models.CharField
price=models.FloatField
count=models.PositiveIntegerField
is_customer=BooleanFiled
model_name=Relation
booking=models.Foreinkey one to many on_delete=models.SET_NULL ,PROTECT,
=models.OntoOne one to one
=models.ManyToMany many to many
example
class Student(models.Model)
name=models.CharField(max_lenght=100)
age=models.PositiveIntegerField()
class Mark(models.Model):
subject=models.CharField(max....)
grade=models.PositiveIntegerField()
Student and Mark
student table
id name age
1 lulu 15
2 sulu 14
mark
id subject grade student
1 maths 10 1
2 arabic 9 1
3 maths 9 2
4 arabic 15 2
class Mark(models.Model):
subject=models.CharField(max....)
grade=models.PositiveIntegerField()
student=models.Foreinkey(Student,)
Serializer
from rest_framework import serializers
class StudentSerializer(serializers.ModelSerailzers):
class Meta:
model = Student
exclude=('created','updated') or fieleds=('name','age') or fields= '__all__'
class StudentSerializer(serializers.ModelSerailzers):
class Meta:
model = Student
exclude=('created','updated') or fieleds=('name','age') or fields= '__all__'
def to_internal_value(self, data):
#input data manipulation
return data
def to_representation(self, instance);
data= super()
#output data manipulation
return data
def validate(self, attrs):
def create(
def update(
views
from rest_framework import viewsets
class StudentViewset(viewsets.ModelViewSet):
queryset.Student.objects.all()
serializer_class=StudentSerializer
urls
from rest_framwork.routers import SimpleRouter
#main_url/function_name/
http://localhost:8000/student/
student is function name mapped to viewset function or class
obj=SimpleRouter()
obj.register(r'student',StudentViewset)
urlpatterns=obj.urls
class StudentViewset(viewsets.ModelViewSet):
queryset.Student.objects.all()
serializer_class=StudentSerializer
authetication_class=
permission_class
inbuild methods with model viewset
def create() ===>POST
def update() ==>PUT,PATCH
def retrieve() ==>GET
def list() ===>GET
def destroy() ==>DELETE
@action(methods=['post'],detail=True)
def custom_function()
eg
@action(methods=['POST'],detail=False,authentication_classes=[],permission_classes=[])
def login(self,request):
pass
urls
from rest_framwork.routers import SimpleRouter
#main_url/function_name/
http://localhost:8000/student/
student is function name mapped to viewset function or class
student ===>StudentViewset
router=SimpleRouter()
router.register(r'student',StudentViewset)
urlpatterns=router.urls
ModelViewSet
class ModelViewSet(mixins.CreateModelMixin,==>POST create
mixins.RetrieveModelMixin, ==>GET, retrieve
mixins.UpdateModelMixin, ==>PUT,PATCH update
mixins.DestroyModelMixin,==>DELETE destroy
mixins.ListModelMixin, =====>GET, list
GenericViewSet)
class ProfileViewset(viewsets.ModelViewSet)
create
update
list retrieve
delete
class ProfileViewsets(mixins.CreateModelMixin,GenericViewSet)
def create() ===>POST
Admin
admin.site.register(Student)
or we can customize with adminModel
example to disable delete option from admin
class StudentModel(admin.ModelAdmin):
def get_actions(self, request):
actions = super(StudentModel, self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
def delete_model(self, request, obj):
return 0
def message_user(self, request, message, level = 30, extra_tags, fail_silently) :
message='delete not allowed'#you can give whatever messeage you want
return super().message_user(request, message, 40, extra_tags, fail_silently)
so then we can resgist Student model like
admin.site.register(Student,StudentModel)
to get more about this overiding functions alt+click on admin.ModelAdmin