Django -以优雅的方式编写Python代码

krcsximq  于 2022-12-01  发布在  Go
关注(0)|答案(1)|浏览(141)

我的情况如下所示:
中models.py:

class singer(models.Model):
        name = models.CharField()
        nickName = models.CharField()
        numSongs= models.IntegerField()

   class writer(models.Model):
        name = models.CharField()
        numBooks = models.IntegerField()

    class weeklyTimeSinger(models.Model):
        singerID = models.ForeignKey(singer, on_delete = models.CASCADE, related_name = 'hook1')
        dayWeek = models.IntegerField() 
        startHour = models.TimeField()
        stopHour = models.TimeField()
    
    class weeklyTimeWriter(models.Model):
        writerID = models.ForeignKey(writer, on_delete = models.CASCADE, related_name = 'hook2')
        dayWeek = models.IntegerField() 
        startHour = models.TimeField()
        stopHour = models.TimeField()

中view.py:

class Filters(APIView):
    def queryFilter(self, querySet, request, singerOtWriter):
            param1 = int(request.GET.get('param1', 0))
            param2 = int(request.GET.get('param2', 0))

        if singerOtWriter == "singer":
             querySet = querySet.filter(weeklyTimeSinger_dayWeek=param1)
             querySet = querySet.filter(weeklyTimeSinger_startHour__lt=param2) 
             querySet = querySet.update(.....
             ....a lot of operation on querySet
        else if singerOtWriter == "writer":
             querySet = querySet.filter(weeklyTimeWriter_dayWeek=param1)
             querySet = querySet.filter(weeklyTimeWriter_startHour__lt=param2) 
             querySet = querySet.update(.....
             ....a lot of operation on querySet, the same of the case singer

       return querySet 

class Artist(Filters):
    def get(self, request):
        querySetSinger = singer.objects.all().annotate(numWorks= F('numSongs'))
        querySetSinger = self.queryFilter(querySetSinger , request, "singer")
        querySetSinger = querySetSinger.values('name', 'numWorks')

        querySetWriter = writer.objects.all().annotate(numWorks= F('numBooks'))
        querySetWriter = self.queryFilter(querySetWriter , request, "writer")
        querySetWriter = querySetWriter.values('name', 'numWorks')

        values = querySetSinger.union(querySetWriter)
        serialized = ArtistSerializers(values, many = True)
        return Response(serialized.data)

在queryFilter函数中,根据singerOtWriter参数,我有两个不同的流。这两个流都很长,并且除了“weeklyTimeWriter”或“weeklyTimeSinger”表名之外都是相同的,我不想重复这些代码行,因为它看起来很脏。我没有报告所有的代码行,但有很多行。
有没有更好、更优雅的方法来重写这些代码并推广这些操作?
谢谢你们。

pw136qt2

pw136qt21#

为歌手/作家保留相同的表格,并使用类型字段。您也可以轻松地进行筛选。
models.py :

artistTypes = (
        ('Singer', 'Singer'),
        ('Writer', 'Writer'),
    )

class artistName(models.Model):
      name = models.CharField()
      nickName = models.CharField()
      artistType = models.CharField(max_length=10, choices=artistTypes)
      numReleases = models.IntegerField()

class timeSpent(models.Model):
    artistID = models.ForeignKey(artistName, on_delete = models.CASCADE)
    dayWeek = models.IntegerField() 
    startHour = models.TimeField()
    stopHour = models.TimeField()

相关问题