简体   繁体   English

DRF使用外键更改Django模型的字段名称值

[英]DRF changing field name values of django models with foreign keys

I followed suggestion from this question But i need to name one field of query_set to date filed of another object 我遵循了这个问题的建议,但我需要将query_set的一个字段命名为另一个对象的日期

My models are 我的模特是

class Choice(models.Model):
    question = models.ForeignKey(Question, related_name='choice', on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text


class ChoiceWithTime(models.Model):
    choiceTime = models.ForeignKey(Choice,related_name='choiceTime', on_delete=models.CASCADE)
    choice_date=models.DateField()

My view 我的观点

class QuestionChoicesViewSet(viewsets.ModelViewSet):
    queryset = Choice.objects.all()
    serializer_class = ChoiceDateSerializer

    def get_queryset(self):
        return Choice.objects.values('choiceTime__choice_date','choice_text').annotate(
            total_votes=Count('choiceTime__choice_date'),
        )

I need to count number of submission in particular dates 我需要计算特定日期的提交数量

I don't know how to name choiceTime__choice_date that serializer recognizes field in query set 我不知道如何命名choserizer识别查询集中的字段的choiceTime__choice_date

class ChoiceDateSerializer(serializers.ModelSerializer):
    choiceTime__choice_date  = serializers.DateTimeField()
    total_votes = serializers.IntegerField()

    class Meta:
        model = Choice
        fields = ('id', 'choice_text','total_votes','choiceTime__choice_date')

i receive 我收到

{
    "choice_text": "ant tower",
    "total_votes": 3,
    "choiceTime__choice_date": "2017-04-20"
}

But i want to recieve 但我想接受

{
    "choice_text": "ant tower",
    "total_votes": 3,
    "choice_date": "2017-04-20"
}

Tried different options with no success. 尝试了不同的选择,但没有成功。 Definitely i am missing the point. 无疑,我错过了重点。 For my purposes it is working, but i want to have well written API. 就我而言,它正在工作,但是我想拥有编写良好的API。

2 option change time submission model? 2选项变更时间提交模型?

class ChoiceWithTime(models.Model):
    choiceTime = models.ForeignKey(Choice,related_name='choiceTime', on_delete=models.CASCADE)
    choice_date=models.DateField()
    coutner = models.IntegerField(default=0)

Is 2 option considers to be better approach to my particular problem? 2选项是否被认为是解决我的特定问题的更好方法? Thanks! 谢谢!

You are receiving a json object, which you add its key value. 您将收到一个json对象,并在其中添加了它的键值。

 for vote_detail in data:
   if vote_detail.choiceTime__choice_date:
      vote_detail.choice_date=vote_detail.choiceTime__choice_date

then serialize and save, a quick solution. 然后序列化并保存,这是一个快速的解决方案。

You could also add to your model the name that you want to call it. 您也可以在模型中添加您要调用的名称。 That's closer to backend and maybe worth delving into. 这更接近后端,也许值得研究。

from django.db.models import Count,F

If anybody finds this problem and this is easiest answer i came up to. 如果有人发现这个问题,这是我想到的最简单的答案。 As it was suggested before passing to serializer change value using model package functions 如建议在使用模型包函数传递给序列化程序更改值之前

class QuestionChoicesViewSet(viewsets.ModelViewSet):
    queryset = Choice.objects.all()
    serializer_class = ChoiceDateSerializer
    def get_queryset(self):
        return Choice.objects.all().annotate(choice_date=F('choiceTime__choice_date')).values('choice_date','choice_text').annotate(
            total_votes=Count('choiceTime__choice_date'),
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM