momo668
momo668

Reputation: 433

Django Rest Framework Model Viewsets 'POST' method not appeared in ALLOW methods after explicitly allowing it

Fairly new to it but have after trying out many different solutions, I still get the following problem where the POST html form would not appear (and POST method not allowed) as seen below, all while using a root superuser

enter image description here

model.py

class Category(models.Model):
    category = models.CharField('category', 
        max_length=100, null=False, blank=False, default="NA")
    def __str__(self) -> str:
        return self.category
    class Meta:
        verbose_name_plural = "categories"

class Item(models.Model):
    name = models.CharField('name', max_length=64)
    slug = models.SlugField(max_length=64, null=True, blank=True)
    icon = models.ImageField(blank=True, upload_to ='uploads/')
    category = models.ForeignKey(
        Category, on_delete=models.CASCADE, default='Uncategorized')
    subcategory = models.CharField(
        max_length=255, null=True, blank=True)
    quantity = models.DecimalField(
        max_digits=9, decimal_places=2)
    suggested_value = models.DecimalField(
        max_digits=6, decimal_places=2, null=False)
    retail_value = models.DecimalField(max_digits=6, decimal_places=2,max_length=30)
    description = models.TextField('description', max_length=255, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    tenant = models.TextField('tenant', default='shared')
    exclude = ('tenant')
    
    class Meta:
        # db_table = "storeItems_item"
        default_permissions = ('view','add', 'change', 'delete')
        ordering = ['name']
        get_latest_by = 'created_at'
    
    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse(
            'storeItems_item_detail', args=(), kwargs={'slug': self.slug})
    
    def save(self, *args, **kwargs):
        # assign permission to item
        group = Group.objects.get(name=self.tenant)
        super(Item, self).save(*args, **kwargs)
        for perm in ['view_item', 'add_item','change_item']:
            assign_perm(perm, group, self)
        # Default retail value
        if not self.retail_value:
            self.retail_value = self.suggested_value()
        super().save(*args, **kwargs)

Everything I've tried with configuring the viewset's permission:

class StoreItemsViewSet(
    viewsets.ModelViewSet, PermissionListMixin,   
    # viewsets.GenericViewSet, mixins.ListModelMixin, 
    # mixins.CreateModelMixin, mixins.UpdateModelMixin,
    ):
    queryset = Item.objects.all()
    serializer_class = StoreItemsSerializer
    # permission_classes = (
    #     BigGroupObjectPermission, BigUserObjectPermission)
    permission_classes = [ItemPermission]

...

    # @action(detail=True, methods=['post', 'delete'])
    # def perform_create(self, serializer):
    #     serializer.save(owner=self.request.user)

...

    # def create(self, request, *args, **kwargs):
    #     instance = super().create(request, *args, **kwargs)
    #     assign_perm("change_item", request.user, instance)
    #     assign_perm("change_item", request.user, instance)
    #     return instance

    # def post(self, request):
    #     return Response()

To creating base permission with full permission:

class FullPermission(BasePermission):
    # def has_permission(self, request, view):
    #     return request.method in SAFE_METHODS
    def has_permission(self, request, view):
        return True

    def has_object_permission(self, request, view, obj):
        return True

Still to no avail. Can anyone point me in the right direction as to why the Rest Framework seemed to work? (All while using Django Guardian's BigGroup/BigUser permissions framework)

Upvotes: 0

Views: 105

Answers (1)

Iren E
Iren E

Reputation: 381

class StoreItemsViewSet(ModelViewSet):
permission_classes = (IsAuthenticated,)

def get_queryset(self):
    return Item.objects.filter(category = "smth").order_by("-name")

def get_serializer_class(self):
    if self.request.method in ("PUT", "PATCH"):
        return StoreItemsUpdateSerializer
    return StoreItemsSerializer

def create(self, request, *args, **kwargs):
    ...... write here your code

def update(self, request, *args, **kwargs):
    ..... rite here your code

Upvotes: 0

Related Questions