many-to-many

Django: m2m_changed not fired when end of relation is deleted

岁酱吖の 提交于 2019-12-12 22:02:38
问题 NOTICE : due to production environment constraints, I must stick to django-1.4 for the moment. I've just made a test to see whether I can hook onto an event when ManyToMany changes. I have a Group model that holds several Item objects. Whenever the items change in any group, I want to do something with concerned Group` instances. from django.db import models from django.db.models.signals import m2m_changed, post_delete, pre_delete class Item(models.Model): name = models.CharField(max_length

Django NameError during ManyToMany field referencing

荒凉一梦 提交于 2019-12-12 21:44:57
问题 I am having a table named PlayCat. which basically stores al the category names of playful activities. such as disco,pool n stuff. So i want these categories to b referenced(ManyToMany) when a user creates a Play arena where he/she can select al the categories it belongs to. Play : class Play(models.Model): shopname=models.CharField(max_length=100) desc=models.CharField(max_length=500,blank=True, null=True) address=models.CharField(max_length=300) category = models.ManyToManyField(PlayCat)

How to add/remove many-to-many relation with entity framework by entity key?

跟風遠走 提交于 2019-12-12 20:54:38
问题 I tried: using (Entities e = new Entities()) { EntityKey key = new EntityKey("Entities.Users", "UserId", 20); User user = new User { EntityKey = key}; Role role = e.Roles.FirstOrDefault(); //role.Users.Attach(user); //throws (when uncommented...) InvalidOperationException: //The object being attached to the source object is not attached to the same ObjectContext as the source object. role.Users.Add(user); //throws InvalidOperationException too: //The object cannot be added to the

Many to many relationship update: Cannot insert duplicate key

≡放荡痞女 提交于 2019-12-12 19:55:47
问题 I am still new in EF code first, so please be lenient with me. I have these entity classes: public class User { public int UserId { get; set; } public string UserName { get; set; } public string EmailAddress { get; set; } public string Password { get; set; } public virtual ICollection<Task> Tasks { get; set; } public virtual ICollection<Task> TaskAssignees { get; set; } public User() { Tasks = new List<Task>(); } } public class Task { public int TaskId { get; set; } public string Name { get;

Rails 3. Order by count of matches (many to many)

守給你的承諾、 提交于 2019-12-12 19:53:04
问题 I have a many to many association between two models: class User < ActiveRecord::Base has_many :user_works has_many :works, through: :user_works end class UserWork < ActiveRecord::Base belongs_to :user belongs_to :work end class Work < ActiveRecord::Base has_many :user_works has_many :users, through: :user_works end I have a filter by works, containing several works (ids). My task is to filter users by works and order them by count of matches. Thanks in advance. 回答1: I guess you need to group

Ordered many-to-many relationship in NHibernate

南楼画角 提交于 2019-12-12 19:15:11
问题 Let's say I have two classes: Item and ItemCollection, where ItemCollection contains an ordered list of Item objects with an index, i.e. the list is ordered in a way specified by the user. Let's also say that they have a many-to-many relationship, an ItemCollection can contain many items and an Item can belong to several ItemCollections. That would, in my head, require three tables in the database. One for Item, one for ItemCollection and one for the ordered mapping. The mapping table would

SQL Server - Get all children of a row in many-to-many relationship?

坚强是说给别人听的谎言 提交于 2019-12-12 17:15:26
问题 I'm trying to write a recursive query in SQL Server that basically lists a parent-child hierarchy from a given parent. A parent can have multiple children and a child can belong to multiple parents so it is stored in a many-to-many relation. I modified the following query from another somewhat related question, however this doesn't go all the way up to the tree and only selects the first level child... DECLARE @ObjectId uniqueidentifier SET @ObjectId = '1A213431-F83D-49E3-B5E2-42AA6EB419F1';

Clean way to get foreign key objects in PHP MySQL query

一笑奈何 提交于 2019-12-12 16:24:59
问题 I use the following code to get a book object from my MySQL database. $q = $pdo->prepare('SELECT book_id "id", book_title "title", book_slug "slug" FROM book WHERE book_id=:id'); $q->bindParam(':id', $id, PDO::PARAM_INT); $q->execute(); $book = $q->fetchObject('Book'); Books have a many-to-many relation to songs, which have a song_id , song_title and song_lyrics , so I have a book_song table with book_id and song_id . I'd like to get all the songs belonging to a book. Is there a clean way to

MySQL Many-To-Many condition which combine AND/OR

戏子无情 提交于 2019-12-12 16:17:46
问题 I have a problem with mysql query, it's complex problem which relates to Mysql join query for multiple "tags" (many-to-many relationship) that matches ALL tags? So I have classic MN table schema: Item 1 | Item1 2 | Item2 ... Category 1 | Category1 2 | Category2 3 | Category3 4 | Category4 ... Item_has_category 1 | 1 ... And question is - How can I get rows, where item has Category1 OR Category2 AND Category3 ? It is for some complex filter and some categories has a special group, which must

Django form linking 2 models by many to many field

感情迁移 提交于 2019-12-12 15:25:02
问题 I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event = models.ManyToManyField(Event, blank=True, null=True) class Event(models.Model): name = models.CharField(max_length=30, unique = True) long_description = models.TextField(blank=True, null=True) I want to create a form that allows me to identify the link between the two models when I add a new entry. This works: class ActorForm(forms.ModelForm): class Meta: model = Actor The form includes