题目:
1、查看并研究ListAPIView工具视图类,继承该类完成汽车资源的群查接口,汽车资源需要有image资源,查看接口image资源的值
2、研究GenericViewSet工具集,继承该类完成汽车资源的单查,群查,单增,单改,单删接口
urls.py
from . import views
from django.conf.urls import url
urlpatterns = [
url(<span class="hljs-string">r'^v2/Cars/$'</span>, views.CarViewList.as_view()),
url(<span class="hljs-string">r'^v3/Cars/$'</span>, views.CarViewGenericViewSet.as_view({<span class="hljs-string">'get'</span>: <span class="hljs-string">'list'</span>})),
url(<span class="hljs-string">r'^v3/Cars/(?P<pk>\d+)$'</span>, views.CarViewGenericViewSet.as_view(
{<span class="hljs-string">'get'</span>: <span class="hljs-string">'retrieve'</span>, <span class="hljs-string">'post'</span>: <span class="hljs-string">'create'</span>, <span class="hljs-string">'put'</span>: <span class="hljs-string">'update'</span>, <span class="hljs-string">'patch'</span>: <span class="hljs-string">'partial_update'</span>, <span class="hljs-string">'delete'</span>: <span class="hljs-string">'destroy'</span>})),
]
views.py
class CarViewList(ListAPIView):
serializer_class = serializers.CarModelSrealizer
queryset = models.Car.objects.filter(is_delete=False).all()
lookup_url_kwarg = 'pk'
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
<span class="hljs-keyword">return</span> self.list(request, *args, **kwargs)
class CarViewGenericViewSet(GenericViewSet, ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin,
DestroyModelMixin):
serializer_class = serializers.CarModelSrealizer
queryset = models.Car.objects.filter(is_delete=False).all()
lookup_url_kwarg = 'pk'
汽车表、汽车商家表、赞助商表、赞助商详情表
class BaseModel(models.Model):
is_delete = models.BooleanField(default=False)
createdTime = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
class Car(BaseModel):
name = models.CharField(max_length=64)
price = models.DecimalField(max_digits=10, decimal_places=2)
image = models.ImageField(upload_to='img', default='media/img/default.png')
brand = models.ForeignKey(to='Brand', db_constraint=False, null=True, on_delete=models.SET_NULL, related_name='cars')
sponsors = models.ManyToManyField(to='Sponsor', db_constraint=False, related_name='cars')
@property
def brand_name(self):
return self.brand.name
@property
def sponsor_list(self):
sponsor_list_temp = []
for sponsor in self.sponsors.all():
sponsor_dic = {
'name': sponsor.name
}
try:
sponsor_dic['phone'] = sponsor.detail.phone
except:
sponsor_dic['phone'] = '未知'
sponsor_list_temp.append(sponsor_dic)
return sponsor_list_temp
class Meta:
verbose_name_plural = '汽车'
def __str__(self):
return self.name
class Brand(BaseModel):
name = models.CharField(max_length=64)
class Meta:
verbose_name_plural = '品牌'
def __str__(self):
return self.name
class Sponsor(BaseModel):
name = models.CharField(max_length=64)
@property
def phone(self):
try:
return self.detail.phone
except:
return "无"
class Meta:
verbose_name_plural = '赞助商'
def __str__(self):
return self.name
class SponsorDetail(BaseModel):
phone = models.CharField(max_length=11)
sponsor = models.OneToOneField(to=Sponsor, db_constraint=False, on_delete=models.CASCADE, related_name='detail')
class Meta:
verbose_name_plural = '赞助商详情'
def __str__(self):
try: # 连表可能会出现问题,所以要异常处理
return self.sponsor.name + '的详情'
except:
return super().__str__()