我从 gin doc 中了解到,您可以将 json 绑定到类似的结构
type Login struct {
User string `form:"user" json:"user" binding:"required"`
Password string `form:"password" json:"password" binding:"required"`
}
func main() {
router := gin.Default()
// Example for binding JSON ({"user": "manu", "password": "123"})
router.POST("/loginJSON", func(c *gin.Context) {
var json Login
if c.BindJSON(&json) == nil {
if json.User == "manu" && json.Password == "123" {
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
} else {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
您始终必须构建一个结构来绑定 JSON。
但是如果有一个非常复杂的JSON数据,我只需要获取其中的一部分,创建一个复杂的struct是一个很大的负担。可以避免id直接解析吗?
在 Django 项目中,当模型更改时,迁移文件也会更改。如果我将迁移目录添加到 git,这会引起其他人的冲突。如果我不添加它,有时在迁移时会引发服务器错误。怎么解决这个\xef\xbc\x9f
\n我正在尝试对我的模型进行分组,如下所示:
class Restaurant(models.Model):
pass
class Order(models.Model):
created = models.DateTimeField(auto_now_add=True)
restaurant = models.ForeignKey('Restaurant')
Run Code Online (Sandbox Code Playgroud)
现在我想知道每天创建了多少订单。这意味着我需要从 DateTime 字段获取日期,然后获取计数。
如果相关,我将以Order这种方式获取查询集:
restaurant = Restaurant.objects.get(id=request.data['restaurant_id'])
orders = restaurant.order_set.filter(created__lte=some_date)
Run Code Online (Sandbox Code Playgroud)
现在,我怎样才能得到我想要的东西呢orders?我尝试过类似的事情:
orders.values('created').annotate(Count('created'))
Run Code Online (Sandbox Code Playgroud)
使用TruncDate等等。
我正在使用 Python 2 和 Django 1.11。
编辑为了更好地表达我的意图。我想要用纯Python实现的东西,如下所示:
orders_by_date = {}
for order in orders:
if orders_by_date.get(datetime.date(orders[0].created.year, orders[0].created.month, orders[0].created.day):
orders_by_date[datetime.date(orders[0].created.year, orders[0].created.month, orders[0].created.day)] += 1
else:
orders_by_date[datetime.date(orders[0].created.year, orders[0].created.month, orders[0].created.day)] = 1
Run Code Online (Sandbox Code Playgroud)
EDIT2我能够成功显示单个日期的计数:
orders.filter(created__date=datetime.date(2018, 6, 8)).aggregate(Count('id'))
Run Code Online (Sandbox Code Playgroud)
给我{'id__count': 3}。现在,按所有日期(而不仅仅是单个日期)进行分组将是完美的。
我正在尝试安装 numpy 但出现此错误请帮助我该怎么办?
ERROR: Exception:
Traceback (most recent call last):
File "c:\users\cutea\appdata\local\programs\python\python38-32\lib\site-packages\pip\_vendor\urllib3\response.py", line 425, in _error_catcher
yield
File "c:\users\cutea\appdata\local\programs\python\python38-32\lib\site-packages\pip\_vendor\urllib3\response.py", line 507, in read
data = self._fp.read(amt) if not fp_closed else b""
File "c:\users\cutea\appdata\local\programs\python\python38-32\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 62, in read
data = self.__fp.read(amt)
File "c:\users\cutea\appdata\local\programs\python\python38-32\lib\http\client.py", line 454, in read
n = self.readinto(b)
File "c:\users\cutea\appdata\local\programs\python\python38-32\lib\http\client.py", line 498, in readinto
n = self.fp.readinto(b)
File "c:\users\cutea\appdata\local\programs\python\python38-32\lib\socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "c:\users\cutea\appdata\local\programs\python\python38-32\lib\ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "c:\users\cutea\appdata\local\programs\python\python38-32\lib\ssl.py", line 1099, in …Run Code Online (Sandbox Code Playgroud) auto_now如果和被混淆就会遇到问题auto_now_add。如何做auto_now或auto_now_add工作?
auto_now:每次使用时都会创建时间models.save(),但models.create()如果使用则不起作用query.update(),它只更新一些数据,但不会自动更新日期
auto_now_addmodels.save():只有第一次使用或时才会创建时间models.create()
应该如何使用它们?
auto_now_add应该与 一起使用created_date并auto_now应该与 一起使用updated_date
created_date = models.DateTimeField(auto_now_add = True)
updated_date = models.DateTimeField(auto_now = True)
Run Code Online (Sandbox Code Playgroud) 任何人都可以帮助我消耗流式传输的连续数据中的数据。主题的雪花连接器中应该给出什么?
因为我能够通过具有所需主题名称的单个表数据来填充数据。但我需要将连续数据流捕获到表中。
我正在尝试复制 pgAdmin4 1.5 中 SQL 表中的现有 CSV 文件。
我正在运行以下查询来复制 CSV 文件中的数据:
COPY console_games FROM '/users/user1/Desktop/ConsoleGames.csv' DELIMITER ',' CSV HEADER;
Run Code Online (Sandbox Code Playgroud)
我得到这个结果:
Run Code Online (Sandbox Code Playgroud)********** Error ********** ERROR: could not open file "/Users/user1/Desktop/ConsoleGames.csv" for reading: Permission denied SQL state: 42501
我已更改此文件的权限以供所有用户读取和写入,但仍然收到错误。
postgresql macos file-permissions permission-denied pgadmin-4
我有一个用户注册 APIView。
看法:
class UserCreateAPIView(CreateAPIView):
serializer_class = UserCreateSerializer
permission_classes = [AllowAny]
queryset = User.objects.all()
Run Code Online (Sandbox Code Playgroud)
序列化器:
class UserCreateSerializer(ModelSerializer):
"""
User register
"""
class Meta:
model = User
fields = [
'username',
'wechat_num',
'password',
]
extra_kwargs = {
"password":{"write_only":True}
}
def create(self, validated_data):
username=validated_data.pop('username')
wechat_num = validated_data.pop('wechat_num')
password=validated_data.pop('password')
user_obj = User(
username=username,
wechat_num=wechat_num,
)
user_obj.set_password(password)
user_obj.save()
group=getOrCreateGroupByName(USER_GROUP_CHOICES.User)
user_obj.groups.add(group)
return validated_data
Run Code Online (Sandbox Code Playgroud)
当我访问这个 APIView 时,我会收到错误:
username/api/users/register/ 处的 KeyError “尝试获取序列化器上字段的值时出现 KeyErrorUserCreateSerializer。\n序列化器字段可能命名不正确,并且与实例上的任何属性或键不匹配dict。\n原始异常文本为:“用户名” '。”
但数据库它会创建用户成功。
全部测试成功:
我已经阅读了 StackOverflow 上可以找到的所有可能的解决方案,并浏览了 allauth 文档。仍然无法指向我的本地(在我的 Django 项目中)模板而不是 allauth 模板(即登录、注册等)
1. 将我的应用程序移到 settings.py INSTALLED_APPS 中的 allauth 之前,使其看起来像这样:
'users', #my custom user model app
'date_track.apps.DateTrackConfig', # main app
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.twitter',
Run Code Online (Sandbox Code Playgroud)
]
2.修改了我的项目结构并将 allauth 模板(登录、注册等)移至此位置。
my_project/templates/allauth/ 在这个目录中我有 3 个 allauth 目录: 1. account 2. openid 3.socialaccount
在这些目录中,我有所有 allauth 模板,加上 base.html
3. 我已将 settings.py 中的模板设置修改为如下所示:
'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR,
'templates', 'allauth','accounts', 'socialaccount')],
Run Code Online (Sandbox Code Playgroud)
然而,每当我访问主页上的链接(继承自我的项目的 base.html)时,它都会直接转到以下位置的模板:site-packages/allauth/templates/account 目录。
base.html 具有模板的链接,如下所示:
{% if user.is_authenticated %}
<li class="nav-item">
<p><h6>You are Logged in as …Run Code Online (Sandbox Code Playgroud) npm install -g create-react-app
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! stack: 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! The operation was rejected by …Run Code Online (Sandbox Code Playgroud) django ×5
python ×2
git ×1
go ×1
go-gin ×1
javascript ×1
json ×1
macos ×1
overriding ×1
parsing ×1
pgadmin-4 ×1
postgresql ×1
python-3.8 ×1
reactjs ×1
snowflake-cloud-data-platform ×1
templates ×1
ubuntu ×1