我已经获得了一个具有控制器(仅限最少代码)和模型的项目,但缺少视图.有没有办法只使用脚手架或其他工具生成视图?
for item in data:
category_id = item['category_id']
del item['category_id']
category = Category.objects.get(pk=category_id)
code = item['code']
try:
article = Article.objects.get(pk=code)
except:
article = Article(**item)
article.save()
# at this point I have the article & category, but the next
# statement throws me an error:
category.articles.add(article)
category.save()
Run Code Online (Sandbox Code Playgroud)
错误是:
AttributeError: 'ManyRelatedManager' object has no attribute 'add'
Run Code Online (Sandbox Code Playgroud) 许多有经验的开发人员建议不要使用Django多表继承,因为它的性能很差:
Django的疑难杂症:混凝土继承由雅各布·卡普兰,莫斯,Django的一个核心因素.
几乎在所有情况下,抽象继承是一种长期更好的方法.我看到在混凝土继承引入的负载下,有很多网站被粉碎,所以我强烈建议Django用户在大量怀疑的情况下使用具体的继承.
Django中的两勺由丹尼尔·格林菲尔德(@pydanny)
多表继承,有时称为"具体继承",作者和许多其他开发人员认为这是一件坏事.我们强烈建议不要使用它.
不惜一切代价,每个人都应该避免多表继承,因为它会增加混乱和大量开销.而不是多表继承,在模型之间使用显式OneToOneFields和ForeignKeys,以便您可以控制何时遍历连接.
但是没有多表继承,我不能轻易
另一个模型中的参考基础模型(必须使用GenericForeignKey或反向依赖);
(随意添加更多)
那么Django中这种继承有什么问题?为什么明确的OneToOneField更好?
性能对JOIN的影响有多大?有没有显示性能差异的基准测试?
不会select_related()让我们连接被调用时控制?
django inheritance models concrete-inheritance multi-table-inheritance
通常更好的做法(以及为什么)验证模型或数据库定义中的属性?
对于(一个微不足道的)例子:
在用户模型中:
validates_presence_of :name
Run Code Online (Sandbox Code Playgroud)
与迁移相比:
t.string :name, :null => false
Run Code Online (Sandbox Code Playgroud)
一方面,将其包含在数据库中似乎更能保证不会出现任何类型的坏数据.另一方面,将其包含在模型中会使事情更加透明,更易于理解,方法是将其分组到代码中.其余的验证.我也考虑过两者兼顾,但这似乎既不干,也不易维护.
我是Ruby on Rails的新手,我显然有一个活跃的记录关联问题,但我不能自己解决它.
鉴于三个模型类及其关联:
# application_form.rb
class ApplicationForm < ActiveRecord::Base
has_many :questions, :through => :form_questions
end
# question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :application_forms, :through => :form_questions
end
# form_question.rb
class FormQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :application_form
belongs_to :question_type
has_many :answers, :through => :form_question_answers
end
Run Code Online (Sandbox Code Playgroud)
但是当我执行控制器向应用程序表单添加问题时,我收到错误:
ActiveRecord::HasManyThroughAssociationNotFoundError in Application_forms#show
Showing app/views/application_forms/show.html.erb where line #9 raised:
Could not find the association :form_questions in model ApplicationForm
Run Code Online (Sandbox Code Playgroud)
谁能指出我做错了什么?
在Typescript接口和类之间有什么不同?我什么时候使用Class?我什么时候使用Interfaces?他们有什么好处?
我需要为我的后端服务器(使用Angular 2执行它)创建某种类型的http请求,例如:},
"fields": {
"project": {
"id": "10000"
},
"summary": "something's wrong",
"issuetype": {
"id": "10000"
},
"assignee": { // not neccesary required
"name": "homer"
},
"reporter": {
"name": "smithers"
},
"priority": { // not neccesary required
"id": "20000"
}
}
Run Code Online (Sandbox Code Playgroud)
我应该用什么来构建这些模型?谢谢!
有没有办法从 Laravel 5中的数据库生成模型?
generator包只创建一个空模型.
我想用模型实例中的数据填充表单.但我的形式比模型少.如果我有这样的代码:
class Item(models.Model)
name = models.CharField(max_length=100)
price = models.PositiveIntegerField()
class ItemForm(forms.Form):
name = forms.CharField()
Run Code Online (Sandbox Code Playgroud)
这个功能有什么问题,它看起来应该是好的?
def bound_form(request, id):
item = Item.objects.get(id=id)
form = ItemForm(item.name)
return render_to_response('bounded_form.html', {'form': form})
Run Code Online (Sandbox Code Playgroud)
我得到这样的错误: AttributeError: 'ItemForm' object has no attribute 'get'
在我的模型中,一些验证是重复的:
validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
validates :name_seo, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
Run Code Online (Sandbox Code Playgroud)
我怎么把它放在mixin中?如果我把它们放在mixin中,我会收到这个错误
app/models/validations.rb:5: undefined method `validates' for Validations:Module (NoMethodError)
Run Code Online (Sandbox Code Playgroud) 我在Backbone.js应用程序中有一组模型.
这是一个项目列表,您可以使用鼠标悬停或使用键盘导航.
如果鼠标悬停,或者键盘导航选择了项目,则它们都会执行相同的操作:将特定项目/模型设置为"已选中".
所以在我的模型中,我有一个基本上叫做的属性
selected: false
Run Code Online (Sandbox Code Playgroud)
当它悬停在上面或用键盘选中时,这将是
selected: true
Run Code Online (Sandbox Code Playgroud)
但是,确保当这一个模型成立时,其他模型都是错误的最佳方法是什么?
我目前正在做一个基本的事情,循环通过集合中的每个模型,然后将选定的模型设置为true.但我想知道是否有更好,更有效的方法呢?
models ×10
django ×3
validation ×2
activerecord ×1
angular ×1
associations ×1
backbone.js ×1
class ×1
collections ×1
database ×1
eloquent ×1
forms ×1
inheritance ×1
interface ×1
javascript ×1
laravel ×1
laravel-5 ×1
many-to-many ×1
ruby ×1
scaffold ×1
typescript ×1
views ×1