电子邮件字段可以用作django rest框架的查找字段吗
模型中的电子邮件字段可以用作RESTAPI的查找字段吗 我有一个用户名为models.CharField、电子邮件为models.EmailField的模型用户作为成员。我想把email设置为viewset中的lookup字段,并使用下面的代码电子邮件字段可以用作django rest框架的查找字段吗,django,django-rest-framework,Django,Django Rest Framework,模型中的电子邮件字段可以用作RESTAPI的查找字段吗 我有一个用户名为models.CharField、电子邮件为models.EmailField的模型用户作为成员。我想把email设置为viewset中的lookup字段,并使用下面的代码 def get_queryset(self): if 'email' in self.kwargs: return User.objects.filter(email=self.kwargs['email']) else:
def get_queryset(self):
if 'email' in self.kwargs:
return User.objects.filter(email=self.kwargs['email'])
else:
return User.objects.all()
lookup_field = 'email'
lookup_url_kwarg = 'email'
但是,由于电子邮件字段始终包含“.”,因此查找失败,因为当前路径与这些邮件中的任何一条都不匹配。如何使用包含“.”的值(例如电子邮件)执行成功的api查找
获取/api/user/abc@def.com在视图集中指定lookup\u value\u regex属性
class FooViewset(ModelViewSet):
queryset = Foo.objects.all()
serializer_class = FooSerializer
lookup_field = 'email'
lookup_url_kwarg = 'email'
lookup_value_regex = '[\w@.]+' # here is the new attribute
注意:不必重写get\u queryset方法。在视图集中指定lookup\u value\u regex属性
class FooViewset(ModelViewSet):
queryset = Foo.objects.all()
serializer_class = FooSerializer
lookup_field = 'email'
lookup_url_kwarg = 'email'
lookup_value_regex = '[\w@.]+' # here is the new attribute
注意:您不必重写get_queryset方法。同样的问题,我如下所述处理 我会使用电子邮件字段进行查找,但不可能在URL端点中使用它 由于我的API中的json包含良好的URL端点以及它们的相关电子邮件,因此我查询json以获得URL端点。 一种手动关系:
import requests, json, subprocess
REQUEST_URL = 'http://127.0.0.1:8000/users/?format=json'
login = 'DjangoLogin'
password = 'DjangoPassWord'
response = requests.get(REQUEST_URL, auth=(login, password))
json_data = response.text.encode('utf-8', 'ignore')
readable_json = json.loads(json_data)
email_reference = YOUR_EMAIL_FIELD
new_firstname = YOUR_FIRSTNAME_FIELD
new_lastname = YOUR_LASTNAME_FIELD
match_count = 0
for results in readable_json['results']:
match_count += 1
if results['email'] == email_reference and results['email'] is not None and match_count != 1:
my_url = results['url']
my_cmd = 'http -a ' + login + ':' + password + ' PUT ' + my_url + ' firstname="' + new_firstname + '"' + ' lastname="' + new_lastname + '"'
p = subprocess.Popen(my_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
更多说明请访问我的网站:
当我搜索了很长时间后,我将这些提示分享到帮助我的帖子中,祝你好运 同样的问题,我处理如下 我会使用电子邮件字段进行查找,但不可能在URL端点中使用它 由于我的API中的json包含良好的URL端点以及它们的相关电子邮件,因此我查询json以获得URL端点。 一种手动关系:
import requests, json, subprocess
REQUEST_URL = 'http://127.0.0.1:8000/users/?format=json'
login = 'DjangoLogin'
password = 'DjangoPassWord'
response = requests.get(REQUEST_URL, auth=(login, password))
json_data = response.text.encode('utf-8', 'ignore')
readable_json = json.loads(json_data)
email_reference = YOUR_EMAIL_FIELD
new_firstname = YOUR_FIRSTNAME_FIELD
new_lastname = YOUR_LASTNAME_FIELD
match_count = 0
for results in readable_json['results']:
match_count += 1
if results['email'] == email_reference and results['email'] is not None and match_count != 1:
my_url = results['url']
my_cmd = 'http -a ' + login + ':' + password + ' PUT ' + my_url + ' firstname="' + new_firstname + '"' + ' lastname="' + new_lastname + '"'
p = subprocess.Popen(my_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
更多说明请访问我的网站:
当我搜索了很长时间后,我将这些提示分享到帮助我的帖子中,祝你好运 显示您的url配置。它可能被定义为接受整数ID进行查找。您应该将其定义为接受任意字符串或仅通过电子邮件显示url配置。它可能被定义为接受整数ID进行查找。您应该将其定义为接受任意字符串或仅接受电子邮件