我正在使用Backbone和Marionette,我正在从后端检索我的模型.
这些模型属于一个集合.问题是:
1)除非在初始化中明确指出,否则永远不会调用validate方法.为什么?
2)当我显式调用validate方法时,它会正确返回我为测试创建的无效模型.但我无法捕捉到"无效"事件.我究竟做错了什么?
这是模型:
var Job = Backbone.Model.extend({
validate: function(attrs){
if (! attrs.title ) {
return "A job should have a title";
}
},
initialize: function(){
this.validate(this.attributes); //manual call to validate
this.on("invalid", function(model, error){ //never executed even when the validate model returns the error string
console.log(error);
});
}
});
Run Code Online (Sandbox Code Playgroud)
这里收藏:
var JobList = Backbone.Collection.extend({
model: Job,
url: '/api/1.0/jobs/',
parse: function(response) {
return response.results;
}
});
Run Code Online (Sandbox Code Playgroud) 我有几个模型通过外键关系相互连接。
这种层次结构中的主要层次结构包含一个所有者字段。
我想为所有这些模型创建一个自定义管理器,根据调用它的模型更改返回的查询集。
我知道经理可以访问self.model以获取它所附加的模型。
Class Main(models.Model)
owner=models.ForeignKey (User)
owned = OwnedManager()
Class Second(models.Model)
main=models.ForeignKey('Main')
owned = OwnedManager()
Class Third(models.Model)
second=models.ForeignKey('Second')
owned = OwnedManager()
Run Code Online (Sandbox Code Playgroud)
我希望我的自定义管理器具有这种行为:
class OwnedManager(models.Manager):
def get_owned_objs(self, owner):
if self.model == 'Main': # WRONG: How do I get the model name?
owned_main = self.filter(owner=owner)
return owned_main
elif self.model == 'Second':
owned_second = self.filter(main__owner=owner)
return owned_second
else:
owned_third = self.filter(second__main__owner=owner)
return owned_third
Run Code Online (Sandbox Code Playgroud)
为了以一致的方式在不同的模型中调用它,如下所示:
main_object.owned.get_owned_objs(owner=user1) # of the Model Main
second_object.owned.get_owned_objs(owner=user1) # of …Run Code Online (Sandbox Code Playgroud) I've manually created a data migration file for a specific Django 1.11 app:
from __future__ import unicode_literals
from django.db import migrations, models
def set_item_things(apps, schema_editor):
MyModel = apps.get_model('my_app', 'MyModel')
# NOTE: if I remove this line then the tests will work
MyOtherModel = apps.get_model('my_other_app', 'MyOtherModel')
for item in MyModel.objects.all():
# NOTE: if I remove this line then the tests will work
thingy = MyOtherModel.get(example_field=item.color)
item.other_thing = thingy
item.save()
class Migration(migrations.Migration):
dependencies = [
('contracts', '0014_my_previous_migration'),
]
operations = [
migrations.RunPython(set_item_things), …Run Code Online (Sandbox Code Playgroud) 我是 Django 的新手,试图构建这个查询让我很头疼。
也许通过图像您可以更好地理解。
我的问题:
给定一个 user.id,我需要知道他所连接的客户的所有其他 user.id(因此通过from_customer和 to_customer):
所以基本上,首先我需要使用反向查找从 User 挖掘到 RelatedCustomer ,获取所有集合,然后返回知道集合中每个客户的user.id。
编辑2:
到目前为止我所达到的:
# This gives me back a customer profile given a user.id (2)
cm = CustomerProfile.objects.get(base_profile__user=2)
# M2M lookup. Given one customer …Run Code Online (Sandbox Code Playgroud) 简洁的问题:
在不使用through参数的情况下,在外部表上对 Django (1.5) 中的多对多关系建模有什么优点和缺点?
详细信息:
说,我有一个自定义用户模型UserProfile,我想定义与同一模型的 m2m 关系,例如实现以下关系。我可以像这样定义一个外部表(模型):
class Relationship(models.Model):
"""Relationship model"""
from_user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='from_users')
to_user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='to_users')
created = models.DateTimeField(auto_now_add=True)
is_blocked = models.BooleanField(default=False)
objects = RelationshipManager()
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我应该向 UserProfile 模型添加一个 m2m 字段,如下所示?如果是,为什么?我只能使用关系模型处理用户之间的所有关系,不是吗?
class UserProfile(AbstractBaseUser, PermissionsMixin):
user_following = models.ManyToManyField('self', through=Relationship, symmetrical=False, related_name='followed')
Run Code Online (Sandbox Code Playgroud) 这是我的用例:
所以基本上我使用了一个非常方便的函数insert_or_update_many:
但这引入了并发问题.例如:如果在步骤1期间不存在对象,则将其添加到稍后要插入的对象列表中.但在此期间可能会发生另一个Celery任务创建该对象,并且当它尝试执行批量插入时(步骤3),我收到重复条目的错误.
我想我需要在'阻塞'块中包含3个步骤.我已经阅读了有关交易的内容,并且我试图将步骤1,2,3包含在一个with transaction.commit_on_success:块内
with transaction.commit_on_success():
cursor.execute(sql, parameters)
existing = set(cursor.fetchall())
if not skip_update:
# Find the objects that need to be updated
update_objects = [o for (o, k) in object_keys if k in existing]
_update_many(model, update_objects, keys=keys, using=using)
# Find the objects that need to be inserted.
insert_objects = [o for (o, k) in object_keys if k not in existing]
# Filter out any …Run Code Online (Sandbox Code Playgroud) 我正在调试Django在pycharm中设置断点.
我对来自ajax的同一资源有多个顺序请求,我无法预测它们到达的完全相同的顺序
Pycharm似乎只停留在第一个,但是从控制台我可以看到实际上所有的请求都发生了并且它们被提供了.
我的难点在于,在某种程度上只有第一个有多个线程和pycharm停止,不确定..
在运行/调试配置下,我检查了"仅单实例"选项,但它似乎没有帮助.
难道我做错了什么?
编辑:
在左下角我找到了"框架"框,从那里我可以切换当前线程.我已经切换了线程并找到了我感兴趣的变量.

这对于调试来说有点复杂.在单进程/线程模式下是否有任何选项可用于调试目的?
在我的模板中,我有这样的事情:
{{formatMyDate(date)}}
Run Code Online (Sandbox Code Playgroud)
但是date是一个不能立即使用的范围变量,因此该表达式将在返回正确的值之前formatMyDate()多次调用该函数(返回undefined).
我可以检查date函数中是否为空,但我想它会更干净如果date是的话根本不调用函数null.
有没有办法实现这个目标?自定义过滤器会帮助我吗?
编辑:
有人建议这种行为可能正常,具体取决于$摘要周期.
然后我用一个scope.$watch来验证价值date变化的次数.
请注意,我在指令中定义了这些.
scope.$watch('date', function(value){
console.log('watched_date: ' + value)
})
Run Code Online (Sandbox Code Playgroud)
我也在我的formatMyDate函数上引入了console.log()
scope.formatMyDate = function(date){
console.log("called_date: " + date)
return dateService.format(date, 'YYYY-MM-DD')
}
Run Code Online (Sandbox Code Playgroud)
检查我得到的控制台(伪代码)
called_date: undefined
watched_date: undefined
called_date: undefined // many many times (around 20/30)
called_date: correctValue //2 or 3 times
watched_date: correctValue
called_date: correctValue //other 3/4 times
Run Code Online (Sandbox Code Playgroud)
我想知道这仍然是由于$digest循环还是我的代码中的错误
我在Babel环境中的ES6中遇到了这个问题:
// A.js
class A {
}
export default new A();
// B.js
import C from './C';
class B {
}
export default new B();
// C.js
import A from './A';
import B from './B';
class C {
constructor(A, B){
this.A = A;
this.B = B; // undefined
}
}
export default new C(A, B)
Run Code Online (Sandbox Code Playgroud)
我像这样导入它们:
// stores/index.js
import A from './A';
import B from './B';
import C from './C';
export {
A,
B,
C
}
Run Code Online (Sandbox Code Playgroud)
从我的应用程序入口点我做:
import * as …Run Code Online (Sandbox Code Playgroud) javascript module circular-dependency ecmascript-6 es6-modules
我有这样的清单:
a = ['orange', 'apple', 'banana']
Run Code Online (Sandbox Code Playgroud)
从这里我使用简单的列表理解构建另一个元组列表:
b = [(key, key.upper()) for key in a]
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
b = [('orange', 'ORANGE'), ('apple', 'APPLE'), ('banana', 'BANANA')]
Run Code Online (Sandbox Code Playgroud)
现在我需要在新生成的b列表的开头(或者例如末尾)插入另一个元组:('---', None).
这两个简单的解决方案是:
# Solution 1
b = [('---', None)] + b
# Solution 2
b.insert(0, ('---', None))
Run Code Online (Sandbox Code Playgroud)
问题:
有没有办法直接从列表推导构造中完成同样的事情?
可以在python中使用这样的(伪代码)吗?
b = [('---', None), (key, key.upper()) for key in a]
Run Code Online (Sandbox Code Playgroud) 我正在使用两台不同的机器,一台Windows和一台Mac Os X开发一个Django项目.我通过Dropbox同步了一切.问题是settings.py中的一些设置是单个字符串(例如MEDIA_ROOT或STATIC_ROOT)而不是元组.这意味着我可以设置一个正确的路径,假设STATIC_ROOT只用于两个操作系统中的一个.在另一个当然它将无法工作.
我想知道是否有一种方法来识别OS python正在运行并根据它选择适当的设置条件.
django ×7
python ×4
django-orm ×2
javascript ×2
angularjs ×1
backbone.js ×1
breakpoints ×1
django-apps ×1
django-users ×1
dropbox ×1
ecmascript-6 ×1
es6-modules ×1
expression ×1
list ×1
marionette ×1
module ×1
pycharm ×1
sql ×1
transactions ×1
validation ×1