Unregister a viewset from drf router

Viewed 76

I have two separate apps Product and Tag which i used another app Product_tags to connect them together. in this way, if one of them don't exists, another one will work fine. inside Product_tags, I created a new TagProductSerializer which inherits ProductSerializer and I just added a new field named tag in fields list.
product_tags/serializers.py:

class TagProductSerializer(ProductSerializer):
    tags = serializers.PrimaryKeyRelatedField(queryset=Tag.objects.all())
    
    class Meta:
        model = Product
        fields = [
            'title',
            'tags',
        ]
    #...

and I did the same with Product viewset
product_tags/views.py

class TagProductViewset(ProductViewset):
    serializer_class  = SocialProductSerializer

and in my product_tags/urls.py I imported my Product router and i wanted to register my product viewset again for router. and there is my problem:
product/urls.py

router = routers.DefaultRouter()
router.register('product', ProductViewset)

urlpatterns = [
    path('', include(router.urls)),
]

product_tags/urls.py (PROBLEM)

from product.urls import router
from .views import TagProductViewset

router.unregister('product') # I'm looking for something like this
router.register('product',TagProductViewset)

NOTE: I want to show the tags when getting product and because of that, I don't want to use different url for getting tag (e.g "api/product/tags/")
First Try:
I tried to register the 'product' again (router.register('product',SocialProductViewset)) but it doesn't works

1 Answers

I fixed my problem but i forgot to say that here. I just created new router in my product_tags/urls.py and then I added it in urlpatterns inside of project/urls.py at top of my original product router.
product_tags/urls.py

from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import TagProductViewset

router = DefaultRouter()
router.register('product',TagProductViewset)

urlpatterns = [
    path('', include(router.urls)),
]

project/urls.py

from django.urls import path, include


urlpatterns = [
    path('api/v1/', include("product_tags.urls")),
    path('api/v1/', include("product.urls")),
]
Related