models

Querying django ManyToMany

孤街醉人 提交于 2019-12-11 07:57:55
问题 I have got Foo <=> FooGroup <=> Bar relation, where <=> stands for ManyToMany field. How do I retrieve all the Foo s for a specific Bar instance? 回答1: Here's an example with auth models, where the relationship is very much like your structure : User <=> Groups <=> Permission from django.contrib.auth import models models.Permission.objects.filter(group__user=models.User.objects.get(username="webmaster")) With your example: Foo.objects.filter(foogroup__bar=barinstance) 来源: https://stackoverflow

ASP.NET MVC - How to populate dropdownlist with another model?

三世轮回 提交于 2019-12-11 07:04:35
问题 one is called PropertyModel and the other is called PropertyTypeModel . The PropertyModel contains a PropertyTypeModel as you can see next: public class PropertyModel { public int PropertyID { get; set; } public PropertyTypeModel PropertyType { get; set; } [DataType(DataType.Text)] [DisplayName("Property name")] public string PropertyName { get; set; } } The PropertyTypeModel is this: public class PropertyTypeModel { public int PropertyTypeID { get; set; } [DataType(DataType.Text)]

request in django models.py

人走茶凉 提交于 2019-12-11 06:52:27
问题 I might be a noob question but... is there a way of using request in models.py? Something like: class MyModel (models.Model): User = models.ForeignKey(default=request.user) ...other fields... Or maybe using the post_init method for doing this job. Thanks. 回答1: That specific example you gave it's not useful in Django. Request do have a context with them (the context where the HTTP happened) so it could or could not be available when you instantiate MyModel . You can do in your view: def index

Complex find query for hasMany relationship in cakePHP 1.3

大城市里の小女人 提交于 2019-12-11 06:34:18
问题 I have been busy with the cakePHP framework for a couple of months now and I really love it. At the moment I'm working on a very new project and it does the job like it should (I think ...) but I feel uncomfortable with some code I wrote. In fact I should optimize my paginate conditions query so I get immediately the right results (right now I manipulate the result set by a bunch of Set::extract method calls. I'll sketch the relevant aspects of the application. I have a model 'Site' who has a

Django model recursive relationship

折月煮酒 提交于 2019-12-11 05:02:06
问题 Why would I create a recursive relationship? aField = models.ForeignKey('self') Is this the same with the above? class aClass(models.Model): aField = models.ForeignKey('aClass') 回答1: You may need to create a recursive relationship when you would like to have parent and child nodes with identical model structure. For example if you have comments with text, data and user_id: class Comment( models.Model ): text = models.TextField() create_date_time = models.DateTimeField() parent_comment =

Updating rails has_many through relations

戏子无情 提交于 2019-12-11 04:06:51
问题 I have models class Agency < ActiveRecord::Base has_many :specializations has_many :cruise_lines, through: :specializations end class CruiseLine < ActiveRecord::Base has_many :specializations has_many :agencies, through: :specializations end class Specialization < ActiveRecord::Base belongs_to :agency, inverse_of: :specializations belongs_to :cruise_line, inverse_of: :specializations end I want to update Specialization collection (that is delete some of old relations and add a few new if

yii2 sorting in related model

给你一囗甜甜゛ 提交于 2019-12-11 02:58:28
问题 have relation model Shop.php public function getShopAddr() { return $this->hasOne(SprShopAddr::className(), ['id' => 'shop_addr_id']); } model SprShopAddr.php public function getDivision() { return $this->hasOne(SprDivision::className(), ['id' => 'division_id']); } model SprDivision.php public function getShopAddrs() { return $this->hasMany(SprShopAddr::className(), ['division_id' => 'id']); } view index.php <?= GridView::widget([ 'dataProvider' => $dataProvider, //'filterModel' =>

Trouble with Rails has_many relationships

∥☆過路亽.° 提交于 2019-12-10 16:51:28
问题 I'm writing an app where a user can both create their own pages for people to post on, and follow posts on pages that users have created. Here is what my model relationships look like at the moment... class User < ActiveRecord::Base has_many :pages has_many :posts has_many :followings has_many :pages, :through => :followings, :source => :user class Page < ActiveRecord::Base has_many :posts belongs_to :user has_many :followings has_many :users, :through => :followings class Following <

rails model attributes without corresponding column in db

岁酱吖の 提交于 2019-12-10 16:12:25
问题 I have some rails models which don't need any persistence, however I'd like rails to think the model actually has attributes x, y, z so when calling methods like to_json in the controller I get them included for free. For example, class ModelWithoutTableColumns << ActiveRecord::Base def x return "Custom stuff here" end There is no column x in the database for Table "ModelWithoutTable" (sorry for the slightly confusing name!) Anyone have an idea how to tackle this one? 回答1: Sounds like you

Models and roles in Qt5 and Qt 4.8

烂漫一生 提交于 2019-12-10 14:18:21
问题 Today I wanted to play with QtQuick2 a little bit. So I have started to port very simple Qt Quick1 app to Quick2. This app uses some models. And in Qt5 models are not working as I expect: I can't access data using roles. This is my QML code: import QtQuick 2.0 Rectangle { width: 800 height: 360 ListView { model: mainModel spacing: 5 anchors.fill: parent orientation: ListView.Vertical delegate: Text { text: "1" Component.onCompleted: { console.log(mainModel); console.log(mainModel.roles() );