Django-piston:我怎样才能获得app_label +型号_名称?

发布于 2024-10-21 20:41:51 字数 187 浏览 2 评论 0原文

在我刚刚使用内置的 django 序列化器之前,它添加了一个模型字段。

{
    pk: 1
    model: "zoo.cat"
}

如何使用 django-piston 获得相同的模型字段?

我尝试了 fields = ('id', 'model') 但这不起作用。

Before I was just using the build-in django serializers and it added a model field.

{
    pk: 1
    model: "zoo.cat"
}

How can I get the same model field using django-piston?

I tried fields = ('id', 'model') but that didn't work.

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

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

发布评论

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

评论(4

不喜欢何必死缠烂打 2024-10-28 20:41:51

将其添加到我的模型中:

def model(self):
    return "{0}.{1}".format(self._meta.app_label, self._meta.object_name).lower()

并将其添加到我的 BaseHandler 中:

fields = ('id', 'model')

似乎有效。如果有人有其他解决方案,请随时发布。

Added this to my model:

def model(self):
    return "{0}.{1}".format(self._meta.app_label, self._meta.object_name).lower()

And this to my BaseHandler:

fields = ('id', 'model')

Seems to work. If anybody has other solutions feel free to post them.

携余温的黄昏 2024-10-28 20:41:51

正如您的代码 app_label:

    instance._meta.app_label

for model_name:

   instance.__class__.__name__

get_model 可以从字符串或网址获取模型名称!

As your code for app_label:

    instance._meta.app_label

for model_name:

   instance.__class__.__name__

and with get_model can get model name from strings or url!

2024-10-28 20:41:51

更好地使用元 Options.label

https ://docs.djangoproject.com/en/2.1/ref/models/options/#label

MyModel._meta.label  # app_name.MyModel
MyModel._meta.label_lower  # app_name.mymodel

Better use the meta Options.label

https://docs.djangoproject.com/en/2.1/ref/models/options/#label

MyModel._meta.label  # app_name.MyModel
MyModel._meta.label_lower  # app_name.mymodel
Smile简单爱 2024-10-28 20:41:51

参考:Alireza Savand 的回答

from django.apps import apps

def get_app_label_and_model_name(instance: object):
"""
    get_model(), which takes two pieces of information — an “app label” and “model name” — and returns the model
     which matches them.
@return: None / Model
"""
app_label = instance._meta.app_label
model_name = instance.__class__.__name__
model = apps.get_model(app_label, model_name)
return model

如何使用?

model_name = get_app_label_and_model_name(pass_model_object_here)

并使用它来获取查询的动态模型名称

model_name = get_app_label_and_model_name(pass_model_object_here)
query_set = model_name.objects.filter() # or anything else

reference: Alireza Savand's answer

from django.apps import apps

def get_app_label_and_model_name(instance: object):
"""
    get_model(), which takes two pieces of information — an “app label” and “model name” — and returns the model
     which matches them.
@return: None / Model
"""
app_label = instance._meta.app_label
model_name = instance.__class__.__name__
model = apps.get_model(app_label, model_name)
return model

How to use?

model_name = get_app_label_and_model_name(pass_model_object_here)

and use this to get dynamic model name for queries

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