Django经常使用API

发布于 2025-01-31 11:47:24 字数 99 浏览 2 评论 0原文

我创建了一个项目和5个应用程序,

现在问题是所有或任何特定用户都使用的特定API

可以帮助我

创建新的API或其他任何东西,但是我需要上述输出

I created a project and 5 applications

Now the problem is how many times the particular api is used by all or any particular user

Can any one help me

We can create new api or anything but I need output for above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜泪 2025-02-07 11:47:24

尝试使用请求/响应信号。
核心框架处理请求时发送的信号。
django.core.signals.request_started
然后添加信号接收器

from django.dispatch import receiver

@receiver(request_finished)
def my_callback(sender, **kwargs):
    print("Request finished!")```

Try to use request/response signals.
Signals sent by the core framework when processing a request.
django.core.signals.request_started
Then add signals receiver like this

from django.dispatch import receiver

@receiver(request_finished)
def my_callback(sender, **kwargs):
    print("Request finished!")```
倦话 2025-02-07 11:47:24

您需要创建一个模型来保存用户的API计数。

from django.db import models
from django.contrib.auth.models import User


class ApiCounts(models.Model):

    api_name = models.CharField(max_length=1024)
    user = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name="api_counts", blank=True
    )
    total_count = models.PositiveBigIntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

使用django middledwares“> middlewares”> middlewares ,在process_request方法中,您可以增加如果请求是身份验证,则为用户的API计数(url和vive_name在请求对象中),否则在用户字段中使用null。

因此,您可以通过查询该模型来显示每用户API计数以及特定API的总计计数。

You need to create a model to save the API counts of the user.

from django.db import models
from django.contrib.auth.models import User


class ApiCounts(models.Model):

    api_name = models.CharField(max_length=1024)
    user = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name="api_counts", blank=True
    )
    total_count = models.PositiveBigIntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Using the django middlewares, inside the process_request method, you can increment the count of API (URL and view_name are inside the request object) for a user if request is authenticated, otherwise use null in the user field.

So, you can show per-user API counts as well as total-counts of a particular API by querying that model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文