Saturday, 9 October 2021

Method POST not allowed [DRF]

When I try to create an UserProfile for User with user_id=2 using the url http://localhost:8000/api/user/profile/2 I get detail : not found

models.py:

class User(AbstractBaseUser, PermissionsMixin, Base):
    user_id = models.AutoField(primary_key=True)
    email = models.EmailField(db_index=True, max_length=100, unique=True)
    is_advisor = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

UserProfile:

class UserProfile(Base):
    profile_id = models.AutoField(primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_profile')
    first_name = models.CharField(null=True, blank=True, max_length=100)
    last_name = models.CharField(null=True, blank=True, max_length=100)

UserProfileSerializer:

class UserProfileSerializer:
    class Meta:
        model = UserProfile
        fields = "__all__"

views.py:

class UserProfileViewSet(viewsets.ModelViewSet):
    queryset = UserProfile.objects.all()
    parser_classes = (MultiPartParser, )
    serializer_class = UserProfileSerializer
    lookup_field = 'user'

urls.py:

router = SimpleRouter(trailing_slash=False)
router.register(r'user', UserViewSet)
router.register(r'user/profile', UserProfileViewSet)
urlpatterns = [
    path(r'user/activate', ActivateUser.as_view(), name='activate_user'),
] + router.urls


from Method POST not allowed [DRF]

No comments:

Post a Comment