我有一个Post和Profile模型。我试图找出category用户帖子列表中最常见的内容。
这是我的模型:
class Post(models.Model):
user = models.ForeignKey(User, blank=True, null=True)
category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='1')
class Profile(models.Model):
user = models.ForeignKey(User, blank=True, null=True)
def most_common_category(self):
posts = Post.objects.filter(user=self.user)
for post in posts:
print(post.category) # 1, 1, 2, 3, 2, 2, 4, 1, 2, 2
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
尝试启动我的Rabbitmq服务器,但出现此错误。我已按照所有说明进行安装。该rabbitmq-server文件位于其中/usr/local/sbin。所以我将目录更改为sbin,并rabbitmq-server在终端中写入内容,但显示错误-bash: rabbitmq-server: command not found。我已经按照说明export PATH=/usr/local/sbin:$PATH在我的底部添加了以下行:bash_profile知道为什么它不起作用吗?
我有一个排序方法,我想根据他们的data-position属性对我的div进行排序.目前,由于某种原因,该功能仅根据第一个数字而不是整数来对div进行排序.这是我的一个例子,html它表明:
HTML
<div class="parent" data-position="1">
...
</div>
<div class="parent" data-position="27">
...
</div>
<div class="parent" data-position="3">
...
</div>
Run Code Online (Sandbox Code Playgroud)
这是js
$('body').on('click', '.new_comments', function(e) {
var divArr = $(".parent");
divArr.sort(function(a, b) {
return $(a).attr('data-position') > $(b).attr('data-position') ? 1: -1;
});
$(".comments_container").append(divArr);
});
Run Code Online (Sandbox Code Playgroud)
知道它为什么这样做以及如何解决它?
我正在学习有关如何在 Django 生产服务器上使用 Celery 的教程。
当我说到这里时:
Now reread the configuration and add the new process:
sudo supervisorctl reread
sudo supervisorctl update
Run Code Online (Sandbox Code Playgroud)
sudo supervisorctl reread当我在我的服务器(Ubuntu 16.04)终端中执行时,它返回以下内容:
ERROR: CANT_REREAD:
The directory named as part of the path /home/app/logs/celery.log does not exist.
in section 'app-celery' (file: '/etc/supervisor/conf.d/app-celery.conf')
Run Code Online (Sandbox Code Playgroud)
我已经完成了之前的所有说明,包括安装supervisor以及app-celery.conf在文件夹中创建名为 mysite-celery.conf ( ) 的文件: /etc/supervisor/conf.d
如果您好奇我的 app-celery.conf 文件如下所示:
[program:app-celery]
command=/home/app/bin/celery worker -A draft1 --loglevel=INFO
directory=/home/app/draft1
user=zorgan
numprocs=1
stdout_logfile=/home/app/logs/celery.log
stderr_logfile=/home/app/logs/celery.log
autostart=true
autorestart=true
startsecs=10
; Need to wait for currently …Run Code Online (Sandbox Code Playgroud) 将数据库从sqlite更改为postgresql后,出现此错误。我已经进行了所有设置更改:
这是我的设置:
DATABASES = {
'default': {
'ENGINE': "django.db.backends.postgresql_psycopg2",
'NAME': "postr1",
'USER': "zorgan",
'PASSWORD': config('DB_PASSWORD'),
'HOST': "localhost",
'PORT': '',
}
}
Run Code Online (Sandbox Code Playgroud)
以及表演makemigrations,migrations都取得了成功。这样我就可以成功启动本地服务器:
System check identified no issues (0 silenced).
May 15, 2018 - 08:59:39
Django version 1.11.8, using settings 'draft1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Run Code Online (Sandbox Code Playgroud)
但是,当我转到该站点时,它将返回此错误:
ProgrammingError at /news/
relation "django_session" does not exist
LINE 1: ...ession_data", "django_session"."expire_date" FROM "django_se...
Run Code Online (Sandbox Code Playgroud)
知道是什么问题吗?
Kotlin 中的意图:
val intent = Intent(this, OtherActivity::class.java)
Run Code Online (Sandbox Code Playgroud)
为什么不能是:
val intent = Intent(this, OtherActivity)
Run Code Online (Sandbox Code Playgroud)
?
在学习Kotlin的过程中,我试图了解Java的Comparator接口的工作原理-主要是compare()函数,以便我可以使用它。
我尝试阅读文档,compare()但我想对其工作原理进行更简单的说明。
究竟是什么x,并y在compare(x, y)当迭代一个列表?迭代时,它是否定位并比较每对数字吗?例如:
arrayOf(1, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)
它会比较1和2(x和y),然后比较2和3(x和y),然后3和4(x和y)吗?
我有一个Kotlin函数,它提供了一个比较器来对列表进行降序排序:
import java.util.*
fun getList(): List<Int> {
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, object: Comparator<Int> {
override fun compare(x: Int, y: Int){
return x < y
}
} )
return arrayList
Run Code Online (Sandbox Code Playgroud)
我不确定为什么上面的函数不是正确的语法来完成此操作。
在我的android项目中,我onCheckedChanged()像这样重写了:
var numberOfPlayers: Int = 0
override fun onCheckedChanged(group: RadioGroup?, checked: Int) {
val chosen = activity?.findViewById<RadioButton>(checked)?.text
numberOfPlayers = chosen.toString().toInt()
}
Run Code Online (Sandbox Code Playgroud)
我很困惑,为什么numberOfPlayers没有红色下划线的chosen可能null-因此我打电话toString()就可能null值。为什么这不会导致 NullPointerException?
我正在服务器端验证我的消费品应用内购买。
也就是说,我通过以下方式从客户端获取收据:
.onChange(of: self.storeObserver.paymentStatus) { status in
switch status {
case .purchasing:
print("Payment status: purchasing")
case .failed:
self.creatingGame = false
print("Payment status: failed")
case .deferred:
print("Payment status: deferred")
case .restored:
print("Payment status: restored")
case .purchased:
// Get the receipt if it's available
if Bundle.main.appStoreReceiptURL == nil {
print("appStoreReceiptURL is nil")
}
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {
do {
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
let receiptString = receiptData.base64EncodedString(options: [])
print("receiptString: \(receiptString)")
// Read receiptData
createGame(receiptString: …Run Code Online (Sandbox Code Playgroud)