many-to-many

Django ManyToManyField ordering using through

天大地大妈咪最大 提交于 2019-11-28 18:45:33
Here is a snippet of how my models are setup: class Profile(models.Model): name = models.CharField(max_length=32) accout = models.ManyToManyField( 'project.Account', through='project.ProfileAccount' ) def __unicode__(self) return self.name class Accounts(models.Model): name = models.CharField(max_length=32) type = models.CharField(max_length=32) class Meta: ordering = ('name',) def __unicode__(self) return self.name class ProfileAccounts(models.Model): profile = models.ForeignKey('project.Profile') account = models.ForeignKey('project.Accounts') number = models.PositiveIntegerField() class

Doctrine's Many-To-Many Self-Referencing and reciprocity

為{幸葍}努か 提交于 2019-11-28 18:22:27
By default, self-referencing ManyToMany relationships under Doctrine involve an owning side and an inverse side, as explained in the documentation . Is there a way to implement a reciprocal association whithout difference between both sides? Following the example in the docs: <?php /** @Entity **/ class User { // ... /** * @ManyToMany(targetEntity="User") **/ private $friends; public function __construct() { $this->friends = new \Doctrine\Common\Collections\ArrayCollection(); } // ... } So, adding entity1 to entity2 s friends implies that entity2 will be in entity1 s friends. There are a

How to properly index a linking table for many-to-many connection in MySQL?

北慕城南 提交于 2019-11-28 18:14:44
Lets say I have a simple many-to-many table between tables "table1" and "table2" that consists from two int fields: "table1-id" and "table2-id". How should I index this linking table? I used to just make a composite primary index (table1-id,table2-id), but I read that this index might not work if you change order of the fields in the query. So what's the optimal solution then - make independent indexes for each field without a primary index? Thanks. Quassnoi It depends on how you search. If you search like this: /* Given a value from table1, find all related values from table2 */ SELECT * FROM

listing objects from ManyToManyField

霸气de小男生 提交于 2019-11-28 16:59:32
问题 I am trying to print a list of all the Conferences and for each conference, print its 3 Speakers. In my template I have: {% if conferences %} <ul> {% for conference in conferences %} <li>{{ conference.date }}</li> {% for speakers in conference.speakers %} <li>{{ conference.speakers }}</li> {% endfor %} {% endfor %} </ul> {% else %} <p>No Conferences</p> {% endif %} in my views.py file I have: from django.shortcuts import render_to_response from youthconf.conference.models import Conference

Django removing object from ManyToMany relationship

亡梦爱人 提交于 2019-11-28 16:55:53
How would I delete an object from a Many-to-Many relationship without removing the actual object? Example: I have the models Moods and Interest . Mood has a many-to-many field interests (which is a models.ManyToManyField(Interest) ). I create an instance of Moods called my_mood . In my_moods 's interests field I have my_interest , meaning >>> my_mood.interests.all() [my_interest, ...] How do I remove my_interest from my_mood without deleting either model instance? In other words, how do I remove the relationship without affecting the related models? my_mood.interests.remove(my_interest) Django

Query a many-to-many relationship with linq/Entity Framework. CodeFirst

我是研究僧i 提交于 2019-11-28 15:51:17
问题 How can I query a many-to-many relationship using Entity Framework code first and linq? The problem is that EF create automatically the relation table. So, I don't have it in my context. This is the relational model: I need a list of Articles for a specific Category_Id, basically replicate something like that: select a.Id, a.Title,a.ShortDescription from Articles a join CategoryArticles ca on ca.Article_Id=a.Id where ca.Category_Id = @parameter However my dbcontext only have : public DbSet

ASP.NET MVC Many to Many relationship, using “My Own” table

空扰寡人 提交于 2019-11-28 13:58:55
I am fairly new to using Code First approach with entity framework and I know that I you have a many to many relationship like the entities below, the EF will create the intermediary table automatically: class Post { ... public virtual ICollection<Category> Categories {get; set;} ... } class Category { ... public virtual ICollection<Post> Posts {get; set;} ... } However, if in the intermediary table I need to have extra data fields, one possible way (which I currently like, maybe because I am unaware of better ways) would be defining a new Entity of my own, like: class Posts_Categories {

Query a ManyToMany relation and display the good result in Symfony with Doctrine

[亡魂溺海] 提交于 2019-11-28 12:42:10
in order to know how it really works, there is an unanswered question from Stack website, and notice that I have the similar problem. In my SQl database , I have two tables: Adverts and Categories Indeed, the Adverts table can contain MANY Categories , and of course a Category can be in many Adverts . So I have a ManyToMany relation between the two tables. in SQL, Doctrine creates me a pivot table named adverts_categories. So far there are no problems , everything is theoretically correct. So, in my SQl database, I have three tables: adverts , adverts_categories and categories like this:

Django admin - How can I add the green plus sign for Many-to-many Field in custom admin form

别来无恙 提交于 2019-11-28 12:29:42
The green plus sign button for adding new instances in the admin form disappears for my MultiSelect field (photos) when I define it in my form. Ie, removing the line with the definition (photos = ...) makes the plus sign appear. However, in order to use a custom Field/Widget I need to figure this out. class GalleryForm(ModelForm): photos = ModelMultipleChoiceField(queryset=Photo.objects.all(), label="Photos") def __init__(self, *args, **kwargs): super(GalleryForm, self).__init__(*args, **kwargs) I've peeked at the Django source code and it seems like I have to wrap my widget in a

Unable to create a constant value of type (type) Only primitive types ('such as Int32, String, and Guid') are supported in this context

限于喜欢 提交于 2019-11-28 12:03:19
I've read ALL of: Unable to create a constant value of type 'System.Object' in Entity Framework Entity Framework - "Unable to create a constant value of type 'Closure type'..." error Entity Framework - Union causes "Unable to create a constant value of type.." Only primitive types ('such as Int32, String, and Guid') are supported in this context and searched a bit more, but still no solution. I've seen that this happens on EF 3.5 and in 4.0 the Contains method should be supported, but I'm in EF 4 but I'm getting this error. I have a photo gallery, where albums can have any number of different