relationship

How do I access the properties of a many-to-many “through” table from a django template?

孤街浪徒 提交于 2019-11-28 03:11:52
From the Django documentation... When you're only dealing with simple many-to-many relationships such as mixing and matching pizzas and toppings, a standard ManyToManyField is all you need. However, sometimes you may need to associate data with the relationship between two models. For example, consider the case of an application tracking the musical groups which musicians belong to. There is a many-to-many relationship between a person and the groups of which they are a member, so you could use a ManyToManyField to represent this relationship. However, there is a lot of detail about the

Laravel save / update many to many relationship

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 02:41:19
Can anyone help me on how to save many to many relationship? I have tasks, user can have many tasks and task can have many users (many to many), What I want to achieve is that in update form admin can assign multiple users to specific task. This is done through html multiple select input name="taskParticipants[]" The catch here is that through the same form (input) you can add/remove users, that's why I have to use sync(). Maybe I should start from the beginning but don't know where to start... This is my User model: public function tasks() { return $this->belongsToMany('Task','user_tasks'); }

Flask Sqlalchemy : relationships between different modules

被刻印的时光 ゝ 提交于 2019-11-28 01:30:02
问题 I'm following the Flask-SQLAlchemy tutorial. I have Flask 0.9, sqlalchemy 0.7.8 and flask-sqlalchemy 0.16 on python 2.6. I'm trying to create a "one to many" relationship, like in their tutorial. class Person(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) addresses = db.relationship('Address', backref='person', lazy='dynamic') class Address(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50)) person_id = db.Column(db

Laravel Polymorphic Relations Has Many Through

寵の児 提交于 2019-11-28 00:03:02
问题 I have a Subscriber Model // Subscriber Model id user_id subscribable_id subscribable_type public function user() { return $this->belongsTo('App\User'); } public function subscribable() { return $this->morphTo(); } And a Topic Model // Topic Model public function subscribers() { return $this->morphMany('App\Subscriber', 'subscribable'); } And I want get all users through subscriber model, to notify them like Notification::send($topic->users, new Notification($topic)); // Topic Model public

Laravel: Get pivot data for specific many to many relation

≯℡__Kan透↙ 提交于 2019-11-27 21:48:56
My User model has many Target and vice versa. Now I've got a given User and given Target and I want to access pivot data from their relation. The pivot column is called type How can I achieve this? On the relationships for both User and Target , tack on a ->withPivot('type') which will instruct Laravel to include that column. Then once you have your result set, you can access the field with $user->pivot->type . If you're not iterating over a collection, but have a user and one of their targets and want the type field, you could use $target = $user->targets->find($targetId) and access the type

ORM SQLAlchemy - 建立一个关系 relationship

荒凉一梦 提交于 2019-11-27 20:32:19
relationship函数是sqlalchemy对关系之间提供的一种便利的调用方式, backref参数则对关系提供反向引用的声明 1 背景 如没有relationship,我们只能像下面这样调用关系数据 如果在User中使用relationship定义addresses属性的话, addresses = relationship('Address')则我们可以直接在User对象中通过addresses属性获得指定用户的所有地址 2 backref属性 大致原理应该就是sqlalchemy 在运行时对Address对象动态的设置了一个指向所属User对象的属性 ,这样就能在实际开发中使逻辑关系更加清晰,代码更加简洁了 2 例子 >>> from sqlalchemy import Column, Integer, String >>> class User(Base): ... __tablename__ = 'users' ... ... id = Column(Integer, primary_key=True) ... name = Column(String) ... fullname = Column(String) ... password = Column(String) ... ... def __repr__(self): ... return "<User

Meaning of “n:m” and “1:n” in database design

懵懂的女人 提交于 2019-11-27 18:40:49
In database design what do n:m and 1:n mean? Does it have anything to do with keys or relationships? m:n is used to denote a many-to-many relationship ( m objects on the other side related to n on the other) while 1:n refers to a one-to-many relationship ( 1 object on the other side related to n on the other). 1:n means 'one-to-many'; you have two tables, and each row of table A may be referenced by any number of rows in table B, but each row in table B can only reference one row in table A (or none at all). n:m (or n:n) means 'many-to-many'; each row in table A can reference many rows in

Saving CoreData to-many relationships in Swift

余生长醉 提交于 2019-11-27 18:36:16
I have a one-to-many relationship that looks like so, I've set up my model classes in a file to match: import CoreData import Foundation class Board: NSManagedObject { @NSManaged var boardColor: String @NSManaged var boardCustomBackground: AnyObject? @NSManaged var boardID: String @NSManaged var boardName: String @NSManaged var lists: NSSet } class List: NSManagedObject { @NSManaged var listID: String @NSManaged var listName: String @NSManaged var board: Board } Because I'm fetching data from multiple JSON endpoints, I have to save my lists seperately from my boards. What I want to do is

UML association vs. composition and detail level

爱⌒轻易说出口 提交于 2019-11-27 18:24:23
Actually, make that a couple of amateur UML questions! When creating a UML diagram to model some domain concepts and you come across a domain concept that "holds" some information about another concept, is it better to hold a stamp/reference to that entity or hold the whole entity in the model itself? Please bear in mind that this is relating to creating a simple high-level model - I'm sure in the implementation stage things would be slightly different. For example, which of the two models below is actually correct? The first one has a composition relationship, with FlightBooking holding the

Adding Relations to Laravel Factory Model

纵然是瞬间 提交于 2019-11-27 18:05:08
问题 I'm trying to add a relation to a factory model to do some database seeding as follows - note I'm trying to add 2 posts to each user public function run() { factory(App\User::class, 50)->create()->each(function($u) { $u->posts()->save(factory(App\Post::class, 2)->make()); }); } But its throwing the following error Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::s ave() must be an instance of Illuminate\Database\Eloquent\Model, instance of Illuminate\Database\Eloquent