【问题标题】:Django 'WSGIRequest' object has no attribute 'data'Django 'WSGIRequest' 对象没有属性 'data'
【发布时间】:2018-07-10 02:27:54
【问题描述】:

我正在尝试向 API 发出发布请求,并将结果保存到我的一个数据库表中。

这是我的代码。

这是我的模型。 patientId 是 MyUser 表的 userId 的外键

class MyUser(AbstractUser):
    userId = models.AutoField(primary_key=True)
    gender = models.CharField(max_length=6, blank=True, null=True)
    nric = models.CharField(max_length=9, blank=True, null=True)
    birthday = models.DateField(blank=True, null=True)
    birthTime = models.TimeField(blank=True, null=True)

class BookAppt(models.Model):
    clinicId = models.CharField(max_length=20)
    patientId = models.ForeignKey(MyUser, on_delete=models.CASCADE)
    scheduleTime = models.DateTimeField(blank=True, null=True)
    ticketNo = models.CharField(max_length=5)
    status = models.CharField(max_length=20)

view.py 。 api url 来自另一个 django 项目

@csrf_exempt
def my_django_view(request):

    if request.method == 'POST':
        r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
    else:
        r = requests.get('http://127.0.0.1:8000/api/makeapp/', data=request.GET)

    if r.status_code == 201 and request.method == 'POST':
        data = r.json()
        print(data)
        patient = request.data['patientId']
        patientId = MyUser.objects.get(id=patient)

        saveget_attrs = {
            "patientId": patientId,
            "clinicId": data["clinicId"],
            "scheduleTime": data["created"],
            "ticketNo": data["ticketNo"],
            "status": data["status"],
        }
        saving = BookAppt.objects.create(**saveget_attrs)

        return HttpResponse(r.text)
    elif r.status_code == 200:  # GET response
        return HttpResponse(r.json())
    else:
        return HttpResponse(r.text)

print(data) 中的结果是这样的。

[31/Jan/2018 10:21:42] "POST /api/makeapp/ HTTP/1.1" 201 139 {'id': 22, 'patientId': 4, 'clinicId': '1', 'date': '2018-07-10', 'time': '08:00 AM', 'created': '2018-01-31 01:21:42', 'ticketNo': 1, 'status': 'Booked'}

但错误就在这里

File "C:\Django project\AppImmuneMe2\customuser\views.py", line 31, in my_django_view patient = request.data['patientId'] AttributeError: 'WSGIRequest' object has no attribute 'data'

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    Django rest 框架有自己的 Request 对象。您需要使用 api_view 装饰器在函数视图中启用此请求类型。

    【讨论】:

    • 当我创建一个视图来调用另一个项目的 api 时,我认为 api_view 不起作用......或者我在这里做一些额外的事情吗?
    • @JinNiiSama api_view 正好适合这种情况。我只是想澄清一下,.request.data 仅适用于 rest 框架请求,而不适用于标准 django HttpRequest。要启用第一个,您需要使用 rest frameworks api view decorator。
    • @JinNiiSama 是的。
    【解决方案2】:

    如果您正在打印data,并且您可以在其中看到patientId,那么您为什么要尝试使用request

    可以这样做

        data = r.json()
        print(data)
        patient = data['patientId']
        patientId = MyUser.objects.get(id=patient)
    

    【讨论】:

    • 对不起,只是为了澄清一些事情。如果我的用户模型 id 已重命名为 userId,它会变成patientId = MyUser.objects.get(userId=patient) 吗?
    • 是的,因为你需要匹配查询中的列名
    • 现在很好用。谢谢。我一直认为请求通常是一切的订书机。
    猜你喜欢
    • 2021-04-30
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多