我正在graphene-django用来构建我的 API。我有一个 DjangoObjectType StoreType,它代表模型商店。这个模型有一个 MultiSelectField 命名opening_days,指示商店在一周中的哪几天营业。为了创建新商店,我使用了这个突变:
class Weekdays(graphene.Enum):
MO = "Mo"
TU = "Tu"
WE = "We"
TH = "Th"
FR = "Fr"
SA = "Sa"
SU = "Su"
class CreateStore(graphene.Mutation):
store = graphene.Field(StoreType)
class Arguments:
opening_days = graphene.Argument(graphene.List(Weekdays))
def mutate(self, info, opening_days):
store = Store(opening_days=opening_days)
store.save()
return CreateStore(store=store)
Run Code Online (Sandbox Code Playgroud)
突变完美地工作。但是,当我尝试查询商店时,我得到了"Expected a value of type \"StoreOpeningDays\" but received: Monday, Tuesday",真正有意义的错误,因为此字段将数据保存为单个字符串,值以逗号分隔。问题在于,石墨烯期待的graphene.List(Weekdays)是无法检索到的指定列表。
有想法该怎么解决这个吗?提前致谢!
我正在关注有关动态编程的本教程,并且正在努力在以下问题中实现记忆:
*编写一个函数canSum(targetSum, numbers),该函数True仅在数组中的数字总和可以达到目标总和时才返回。数组中的所有数字都是正整数,您可以多次使用它们来求解。
例子:
canSum(7, [2, 4]) -> False 因为你不能通过添加 2 和 4 来形成 7。*
我的蛮力解决方案如下:
def canSum(targetSum, numbers):
if targetSum == 0:
return True
if targetSum < 0:
return False
for n in numbers:
remainder = targetSum - n
if canSum(remainder, numbers):
return True
return False
print(canSum(7, [2, 3])) # True
print(canSum(7, [5, 3, 4, 7])) # True
print(canSum(7, [2, 4])) # False
print(canSum(8, [2, 3, 5])) # True
Run Code Online (Sandbox Code Playgroud)
效果很好,但如果我们记住余数的解决方案会更快(这在视频中的 1:28:03 分钟进行了解释)。我用 Python 做了以下事情,这正是讲师正在做的事情,但它只会返回True …
我有一个带有 GraphQL 服务器的 React Native 项目。我正在使用 Apollo Client 从我的 API 获取数据。我创建了这个简单的组件,它从路由参数中获取注册所需的所有数据(形成逐步注册),并进行突变以创建新商店。
export default function AuthRegisterStore({ navigation, route }) {
const [
register,
{ loading: validating, error: validationError, data: returnData },
] = useMutation(REGISTER, { onError: (e) => Alert.alert(String(e)) });
const handleRegistration = () => {
register({
variables: {
username: route.params.username,
password: route.params.password,
latitude: route.params.latitude,
longitude: route.params.longitude,
},
});
};
handleRegistration()
if (validating) {
return <Loader />;
}
if (validationError) {
return (
<View style={styles.container}>
<Text>Error</Text>
</View>
);
}
if (returnData) …Run Code Online (Sandbox Code Playgroud) 我正在用 Django 和 Python 3.7 构建一个 web 应用程序,我真的被这个简单的事情搞糊涂了:
这些是我的模板。它们都在同一个目录中。当我尝试调用 {% extends 'store.html' %} 时,我得到TemplateDoesNotExist at /publicaciones/并指向store.html. 这是在publicaciones.html. 这是模板:
publicaciones.html:
{% extends "store.html" %}
{% load static %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static 'css/style_reset-pass.css' %}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Publicaciones</title>
</head>
<body>
<div class="container">
{% block content %}
{% for item in items %}
{{ item|crispy }}
{% endfor …Run Code Online (Sandbox Code Playgroud) python ×3
django ×2
javascript ×2
apollo ×1
expo ×1
graphql ×1
python-3.x ×1
react-native ×1
reactjs ×1