Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

models.py from django.db import models from django.contrib.auth.models import AbstractUser from django.core.validators import MinLengthValidator, MaxLengthValidator class User ( AbstractUser ) : phone _ number = models.CharField

models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import MinLengthValidator, MaxLengthValidator
class User(AbstractUser):
phone_number = models.CharField(max_length=15, unique=True, validators=[MinLengthValidator(10), MaxLengthValidator(15)])
email = models.EmailField(blank=True)
groups = models.ManyToManyField(
'auth.Group',
related_name='custom_user_set', # Add a unique related_name
blank=True,
help_text='The groups this user belongs to.',
verbose_name='groups',
)
user_permissions = models.ManyToManyField(
'auth.Permission',
related_name='custom_user_set', # Add a unique related_name
blank=True,
help_text='Specific permissions for this user.',
verbose_name='user permissions',
)
def __str__(self):
return self.username
class Contact(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
phone_number = models.CharField(max_length=15)
def __str__(self):
return self.name
class SpamReport(models.Model):
reporter = models.ForeignKey(User, on_delete=models.CASCADE)
phone_number = models.CharField(max_length=15)
def __str__(self):
return self.phone_number
serializers.py
from rest_framework import serializers
from .models import User, Contact, SpamReport
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields =('id', 'username', 'phone_number', 'email')
class ContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields =('id', 'user', 'name', 'phone_number')
class SpamReportSerializer(serializers.ModelSerializer):
class Meta:
model = SpamReport
fields =('id', 'reporter', 'phone_number')
urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import UserViewSet, ContactViewSet, SpamReportViewSet
router = DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'contacts', ContactViewSet)
router.register(r'spamreports', SpamReportViewSet)
urlpatterns =[
path('', include(router.urls)),
]
views.py
from rest_framework import viewsets, filters
from .models import User, Contact, SpamReport
from .serializers import UserSerializer, ContactSerializer, SpamReportSerializer
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
filter_backends =[filters.SearchFilter]
search_fields =['username', 'phone_number']
class ContactViewSet(viewsets.ModelViewSet):
queryset = Contact.objects.all()
serializer_class = ContactSerializer
class SpamReportViewSet(viewsets.ModelViewSet):
queryset = SpamReport.objects.all()
serializer_class = SpamReportSerializer
Check the above code for plagarism and if plagarism is found redo the code that will give plagarism less than 5%

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions

Question

Differentiate between business risk and financial risk. (6)

Answered: 1 week ago