我正在使用Django與Restframework,並且我想更新某個對象。我有以下型號:Django Restframework:更新一個對象,但不訪問特定對象ID
class PromotionalCode(models.Model):
promotional_code = models.CharField(max_length=10, default=random_with_letters(), unique=True)
myuser = models.ForeignKey('MyUser', related_name='promotional_code_user', blank=True, null=True)
promotion_type = models.PositiveIntegerField(default=1)
time_transaction = models.DateTimeField(default=datetime.now())
used = models.BooleanField(default=False)
以下視圖集:
class UpdateOnlyDetailViewSet(mixins.UpdateModelMixin,
viewsets.GenericViewSet):
pass
以下觀點:
class PromotionalCodeViewSet(UpdateOnlyDetailViewSet):
queryset = PromotionalCode.objects.all()
serializer_class = PromotionalCodeSerializer
permission_classes = (IsOwnerOrReadOnly,)
而且它具有以下網址:
router = DefaultRouter()
router.register(r'promotionalcode', views.PromotionalCodeViewSet)
我只能更新Promotionalcod Ë對象時我訪問
/promotionalcode/code_id
,我想,當我訪問
/promotionalcode更新某一碼/
而不指定id。我需要做的是做什麼?
你怎麼打算找出哪些對象,而其ID更新? – miki725
選擇與推出的促銷代碼相匹配的促銷代碼 – david