Django Rest Framework:{“ detail”:“未找到。” }使用旧版数据库

时间:2019-04-22 03:52:34

标签: django django-rest-framework django-views

当我使用邮递员发送http://localhost:8888/api/v1/IxtUser/abcd@email.com时,

休息框架来构建一个api {“ detail”:“未找到。” }

abcd@email.com 是主键,并且即时通讯使用的是旧版数据库。

**我已经尝试将'user_id'更改为models.Emailfield(),但是我仍然遇到相同的错误

赔率

在另一个端点中,我尝试发送http://localhost:8888/api/v1/MCustomerData/celc/并像这样

{
    "cust_id": "celc",
    "cust_name": "Celcom",
    "cust_country": "Malaysia",
    "cu": "ECM"
}

我的代码

型号:

class IxtUser(models.Model):
    user_id = models.CharField(max_length=70, primary_key=True, unique=True)
    user_uuid = models.CharField(max_length=50)
    user_passwd = models.CharField(max_length=50)
    user_fname = models.CharField(max_length=50)
    user_lname = models.CharField(max_length=50)
    user_contact = models.CharField(max_length=15)
    user_cu_id = models.CharField(max_length=30)
    user_cust_id = models.CharField(max_length=30)
    user_asp_id = models.CharField(max_length=30)
    user_join_date = models.DateTimeField(blank=True, null=True)
    user_status = models.SmallIntegerField()
    user_parent = models.SmallIntegerField()
    user_monitor = models.SmallIntegerField()
    role2monitor = models.SmallIntegerField(blank=True, null=True)
    pb_lower_counter = models.CharField(max_length=8)
    is_rootpanel_enable = models.SmallIntegerField()

    def __str__(self):
        return self.user_id

    class Meta:
        managed = False
        db_table = 'ixt_user'

观看次数:

class IxtUserFilter(filters.FilterSet):
    class Meta:
        model = IxtUser
        fields = [
            'user_id', 'user_uuid', 'user_fname', 
            'user_lname', 'user_cu_id', 
            'user_cust_id', 'user_asp_id'
                ]

class IxtUserView(viewsets.ModelViewSet):
    queryset = IxtUser.objects.all()
    serializer_class = IxtUserSerializer
    lookup_field = 'user_id'
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_class = IxtUserFilter
    throttle_classes = (UserRateThrottle,)

    def get(self, request, format=None):
        content = {
            'status': 'request was permitted'
        }
        return Response(content)

网址:

from rest_framework import routers
...
from user.views import IxtUserView
router = routers.DefaultRouter()

router.register(r'api/v1/IxtUser', IxtUserView)

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'', include(router.urls)),
    path(r'api/v1/', include('rest_framework.urls', namespace='rest_framework')),
]

1 个答案:

答案 0 :(得分:0)

您的lookup_field设置为用户id。您需要使用user_id来获取请求以获取对象。 http://localhost:8888/api/v1/IxtUser/1 与检索lookup_field无关的主键正在完成所有繁重的工作。如果要检索电子邮件字段上的对象,请将lookup_field设置为email字段