many-to-many

Programmatically identify django many-to-many links

烈酒焚心 提交于 2019-12-11 09:05:45
问题 I'd like to automatically generate a list of an objects many-to-many links. Let's say I have 5 Models: Model 1 has a M2M link to Model 2 Model 2 has a M2M link to Models 3 and 4 Model 4 has a M2M link to Model 5 If the user adds an object to Model 1, I want to give them the option to add another Model 1 or add Model 2 If the user adds an object to Model 2, I want to give them the option to add another Model 2, or add a Model 1, 3, or 4. If the user adds an object to Model 4, I want to give

Ebean multiple @ManyToMany delivers strange behavior

无人久伴 提交于 2019-12-11 08:46:57
问题 I experience strange behavior with Ebean (version 3.2.2), whenever I try to use multiple @ManyToMany attributes with CascadeType.ALL in a model. I've made a simplified example to reproduce the problem: I have the following two Entities (getters omitted) @Entity public class Foo extends Model { @Id @GeneratedValue private Long id; @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "foo_bar1") private List<Bar> bars1; @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "foo_bar2")

How to query for empty records in many to many relation

我是研究僧i 提交于 2019-12-11 08:44:15
问题 class BlogPost(SchemaBase): id = Column(Integer, primary_key=True) name = Column(String, unique=True) authors = relationship('Authors', secondary='authors_to_blog_post') __tablename__ = 'blogpost' class Authors(SchemaBase): id = Column(Integer, primary_key=True) name = Column(String, unique=True) __tablename__ = 'author' authors_to_blog_post = Table('authors_to_blog_post', Base.metadata, Column('author_id', Integer, ForeignKey('author.id')), Column('blogpost_id', Integer, ForeignKey('blogpost

Django, sum of fields in intermediary model with given queryset

℡╲_俬逩灬. 提交于 2019-12-11 08:32:30
问题 I have 2 models, one of them has many to many relation with itself through other table like this. class a(models.Model): # fields class b(models.Model): from_a = models.ForeignKey(a) to_a = models.ForeignKey(a) count = models.PositiveIntegerField() Now, what I wonder is, what is the best way of calculating sum of counts in b's where from_a is "something". This one seems trivial, but I can't figure it out. 回答1: from django.db.models import Sum b.objects.filter(from_a__whatever='something')

How to build a user/group/achievement database?

情到浓时终转凉″ 提交于 2019-12-11 08:24:22
问题 I'm working on a new web platform that has users, groups and those users can get achievements (each group has its own achievements) for doing specific tasks and these are the tables I have at the moment: Users Groups Group_users (used to store user's groups, since users can join more than one group) Achievements User_achievements (used to store user's achievements, since users can have more than one achievement) My question is, in the User_achievements table, should I store the user and group

Laravel ManyToMany Multiple

旧城冷巷雨未停 提交于 2019-12-11 08:23:00
问题 I have 3 models: User , Role , Tool where each user could have many roles, and each role could have many tools. The many to many relationships work well in each case. I can access: User::find(1)->roles Tool::find(1)->roles Role::find(1)->tools Role::find(1)->users My tables are: users id name roles id name tools is name role_user id role_id user_id role_tool id role_id tool_id In each model: //In User Model public function roles() { return $this->belongsToMany('Rol'); } //In Role Model public

Laravel - Polymorphic many-to-many returns empty

对着背影说爱祢 提交于 2019-12-11 08:20:12
问题 I've been at this for 5 hours now and can't seem to figure out the mistake. I'm using Polymorphic Many-to-Many Relations in Laravel 4.1. A Job and an Event each can have a Tag and I've set up the stuff accordingly. But whenever I call $job->tags it returns empty. I have these 3 classes (BaseModel extends Eloquent, namespace is always App\Models, I have to reference them in the e.g. "morphToMany" function, doesn't work with "use"): class Job extends BaseModel { public function tags() { return

Get id's of a ManyToMany mapping table

徘徊边缘 提交于 2019-12-11 08:14:43
问题 I'm writing an API using Spring Boot and Hibernate where my persisted entity objects are also used as DTOs sent to and from the client. This is a simplified version of a typical entity I use: @Entity @Table(name = "STUDENT") public class Student { @Id @GeneratedValue @Column(name = "ID") private Long id; @ElementCollection @CollectionTable(name = "GROUP_STUDENT", joinColumns = @JoinColumn(name = "GROUP_ID")) @Column(name="STUDENT_ID") private Set<Long> groupIds; @JsonIgnore @ManyToMany(fetch

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

Allowing Symfony 4 and Doctrine for duplicate ManyToMany relations

只谈情不闲聊 提交于 2019-12-11 07:54:45
问题 I want to create ManyToMany relation with duplicate options. One USER can have many CARS and many CARS can belong to various USERs. At the same time, one USER can own many cars of the same type. How do I solve this in Symfony 4 and Doctrine? 回答1: Presumably you have two entities, as described, User and Car , and define a ManyToMany relation between them. It could look like this: User.php class User { // ... /** * @ManyToMany(targetEntity="App\Entity\Car", inversedBy="users") */ private $cars;