我正在构建一个项目,在该项目中,我需要一些自定义设置,这些特定设置可由某些无法通过Angular前端访问该应用程序的django管理面板访问的用户编辑。
我安装了django-constance以便编辑这些自定义设置,因为它是我在Django packages site上可以找到的最推荐的软件包。我现在的问题是我不知道如何在我的API rest上提供设置。
我从一些自定义视图开始:
class ConstanceSettingSerializer(serializers.Serializer):
name = serializers.CharField()
value = serializers.Field()
defult_value = serializers.Field()
modified = serializers.BooleanField(read_only=True)
from constance import settings as constance_settings
class ConstanceView(APIView):
def get(self, request):
settings = constance_settings.CONFIG.items()
return Response(settings, status=status.HTTP_200_OK)
但是现在我不知道如何将那些设置正确地传递给我的序列化器。我尝试失败了:
settings_serializer = ConstanceSettingSerializer(settings)
return Response(settings_serializer.data, status=status.HTTP_200_OK)
我只得到错误。
有些想法如何正确地做到这一点?
谢谢!
答案 0 :(得分:1)
给你一个演示
{
"id": 0003,
"parent_id": 0,
"number": "0003",
"order_key": "wc_order_dsadsa003",
"created_via": "checkout",
"version": "3.3.1",
"status": "processing",
"currency": "dollar",
"date_created": "2018-06-27T02:52:40",
"date_created_gmt": "2018-06-27T02:52:40",
"date_modified": "2018-06-27T02:52:41",
"date_modified_gmt": "2018-06-27T02:52:41",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "0.00",
"total": "448.00",
"total_tax": "0.00",
"prices_include_tax": false,
"customer_id": 1,
"customer_ip_address": "127.0.0.1",
"customer_user_agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/67.0.3396.99 safari/537.36",
"customer_note": "",
"billing": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "adasd",
"address_2": "",
"city": "asdas",
"state": "",
"postcode": "8000",
"country": "",
"email": "johndoe@gmail.com",
"phone": "1213123"
},
在utils.py中:
from .utils import get_settings
from constance import config
from rest_framework.viewsets import ViewSet
class SettingViewSet(ViewSet):
permission_classes = (IsAuthenticated,)
def setting(self, request, allow_settings):
if request.method == 'GET':
# list all setting items
return Response(data=get_settings(allow_settings))
else:
# change all allow setting items in allow_settings
for key in request.data:
if key in allow_settings and key in getattr(settings, 'CONSTANCE_CONFIG', {}):
value = request.data[key]
setattr(config, key, '' if value is None else value)
return Response(data=get_settings(allow_settings))
def create(self, request):
"""
<p>update with POST:<code>{'Key': new_value}</code>
"""
allow_settings = [key for key, options in getattr(settings, 'CONSTANCE_CONFIG', {}).items()]
return self.setting(request, allow_settings)
def list(self, request):
"""
get all setting item
"""
allow_settings = [key for key, options in getattr(settings, 'CONSTANCE_CONFIG', {}).items()]
return self.setting(request, allow_settings)
在urls.py中:
def get_settings(allow_settings):
setting_list = []
for key, options in getattr(settings, 'CONSTANCE_CONFIG', {}).items():
if key in allow_settings:
default, help_text = options[0], options[1]
data = {'key': key,
'default': default,
'help_text': help_text,
'value': getattr(config, key)}
setting_list.append(data)
return setting_list
询问获得router.register(r'setting', SettingViewSet, base_name='setting')
,您可以获得:
http://x.x.x.x/setting/
使用数据[
{
"key": "staff_prefix",
"default": "",
"help_text": "工号固定代码",
"value": ""
},
{
"key": "staff_suffix",
"default": 3,
"help_text": "工号尾缀",
"value": "3"
}
]
发布http://x.x.x.x/setting/
,您可以在{'staff_prefix': 'demo'}
中更新值。
答案 1 :(得分:0)
您的序列化器需要知道返回什么。因此,对于每个字段,您都可以执行以下操作:
class ConstanceSettingSerializer(serializers.Serializer):
setting_name = serializers.SerializerMethodField()
def get_setting_name(self):
settings = constance_settings.CONFIG.items()
# Retrieve your setting's value here and return
return setting_value
那是出路。如果您希望能够通过PUT / POST / PATCH更改设置,则需要在序列化程序中覆盖create / update,并实现自己保存值的逻辑。
另一种方法是检查如何实现设置的存储。 我没有使用过constance设置,所以我不能说。但是,如果它使用DB存储设置,则可以为它们使用的模型实现模型序列化器。