0

我将 all_auth 和 rest_auth 与 Angular 一起使用。我有一个名为 Campaign 的模型

class Campaign(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=100)
    hashtag = models.CharField(max_length=20)
    message = models.CharField(null=True, blank=True, max_length=100)
    sale_limit = models.IntegerField(null=True, blank=True)
    start_date = models.DateField(null=True, blank=True)
    expiry_date = models.DateField(default=now_plus_30)
    product_url = models.CharField(max_length=100)
    created_at = models.DateTimeField(default=datetime.now, blank=True)
    modified_at = models.DateTimeField(auto_now_add=True, blank=True)
    user = models.ForeignKey(User, unique=False)
    def __str__(self):              # __unicode__ on Python 2
        return self.name

并认为

class ListCreateCampaign(APIView):
    """
    List all Campaigns or create one.
    """

    def post(self, request, format=None):
        serializer = CampaignSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

我不想通过 API 传递 user_id,如何获取用户详细信息并将其注入 request.data

4

1 回答 1

0

一种方法可能是,user通过将required=False. .save()然后在调用序列化程序的方法时传递用户,例如.save(user=request.user). 欲了解更多信息http://www.django-rest-framework.org/api-guide/serializers/#passing-additional-attributes-to-save

于 2015-12-12T08:30:04.333 回答