many-to-many

How do I make many-to-many field optional in Django?

二次信任 提交于 2019-11-29 20:31:44
When you have a many-to-many relationship ( related_name , not through ) and you are trying to use the admin interface you are required to enter one of the relationships even though it does not have to exist for you to create the first entry. I'm creating an app that is an event organizer. Imagine we had Event and Group models, bound with many-to-many relationship. Django related_name creates another table with the indices of the two other tables. But I see no reason why this extra table has to be populated. If I work with the database through phpMyAdmin I can create a Group without

Save a many-to-many model in Django/REST?

匆匆过客 提交于 2019-11-29 20:02:42
问题 I'm writing a REST API for my Django app, and can't get POST requests to work on one model. Here's the model in question: class ProjectNode(models.Model): name = models.CharField(max_length=60) place = models.CharField(max_length=150) time_spent = models.BigIntegerField() parent_project = models.ForeignKey(Project, related_name='tasks') workers = models.ManyToManyField(User, related_name='tasks_can_do') def __str__(self): return self.name The User model just holds a name field at the moment.

JPA Hibernate many-to-many cascading

旧巷老猫 提交于 2019-11-29 19:59:03
I am using JPA 2.0 and hibernate. I have a User class and a Group class as follows: public class User implements Serializable { @Id @Column(name="USER_ID") private String userId; @ManyToMany @JoinTable(name = "USER_GROUP", joinColumns = { @JoinColumn(name = "GROUP_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") } ) private Set<Group> groupList; //get set methods } public class Group { @Id @Column(name="GROUP_ID") private String groupId; @ManyToMany(mappedBy="groupList") private Set<User> memberList; //get set methods } And then, I create a user and group and then assign the user

Undelete an entity marked as EntityState.Delete?

不问归期 提交于 2019-11-29 19:57:25
问题 instead of talking let me talk with code: Dim Contact = Context.Contacts.Include("Phones") Dim phone = Contact.Phones(0) Contact.Remove(phone) How do I refresh the context now, canceling last relation deletion? I tried: Context.Refresh(RefreshMode.StoreWins, phone) 'Doesn't recover the relation Context.Refresh(RefreshMode.StoreWins, _ ObjectStateManager.GetObjectStateEntries(EntityState.Deleted)) the last one throws an InvalidOperationException: The element at index 0 in the collection of

Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?

…衆ロ難τιáo~ 提交于 2019-11-29 19:22:39
I'm having a little difficulty getting my head around relationships in Django models. Could someone explain what the difference is between a OneToOne, ManyToMany and ForeignKey? Well, there's essentially two questions here: What is the difference (in general) between one to one, many to many, and foreign key relations What are their differences specific to Django. Both of these questions are quite easily answered through a simple Google search, but as I cannot find an exact dupe of this question on SO, I'll go ahead and answer. Note that in Django, relationships should only be defined on one

mysql query show multiple tables from one ID column

无人久伴 提交于 2019-11-29 18:18:38
I'm trying to find this query where I want to show which hosts uses which template from my Zabbix table. The only problem is that hosts and templates are registered in the same table. They are mixed in the table with for example ID 11813 being a host and 11815 being a template. Now I've found a table where the relation between these 2 is defined: hosts_templates. This table has 3 columns: a host_template id, hostid, templateid The table hosts has many columns but also containing: hostid, name where hostid contains the hosts as well as the templates. the table hosts does have a templateid

Inserting multiple entities into many-to-many linking table

梦想的初衷 提交于 2019-11-29 18:15:33
My web application has a many-to-many link between the Station and Timetable entities, and the entity that links them is called StationTimetable , which also holds data about the linkage between the two entities. When I create a new Timetable , I want to create new (and multiple) StationTimetable entries in the database to correspond to this new Timetable . So far I've managed to get the application to save just one StationTimetable entry when the user creates a new timetable. I sort of understand why this is, and I've attempted to modify it to save multiple entries however I'm now hitting a

How to delete bidirectional many-to-many association

走远了吗. 提交于 2019-11-29 17:34:13
问题 Problem: I have many-to-many association between two entities A and B . I set A entity as an owner of their relationship (inverse=true is on A's collection in b.hbm.xml). When i delete an A entity , corresponding records in join table are deleted . When i delete an B entity , corresponding records in join table are not deleted (integrity violation exception). -- Let's consider some very simple example : class A{ Set<B> bset=new HashSet<B>(); //... } class B{ Set<A> aset=new HashSet<A>(); //..

How to concatenate data from one field, in a comma-delimited list, in a many-to-many relationship in MySQL?

馋奶兔 提交于 2019-11-29 17:26:56
问题 I have a many-to-many relationship between People and Departments since one person can be in many departments. People Departments ------ ----------- pID pName deptID deptName 1 James 1 Engineering 2 Mary 2 Research 3 Paul 3 Marketing 4 Communications People_Departments ------------------ pID deptID 1 1 1 2 2 2 2 4 3 1 3 2 3 3 What I want is this: pName deptName James Engineering, Research Mary Research, Communication Paul Engineering, Research, Marketing If I do plain LEFT JOINs on the tables

Way to allow for duplicate many-to-many entries in Python/Django

你离开我真会死。 提交于 2019-11-29 15:20:51
I have the following Django model: class Icon(models.Model): name = models.CharField(max_length=200,null=False,blank=False) class Post(models.Model): icons = models.ManyToManyField(Icon) When I write the following code: post = Post() icons = [] icon_id = form.cleaned_data['icon_1'] if (icon_id): i = Icon.objects.get(id=icon_id) icons.append(i) icon_id = form.cleaned_data['icon_2'] if (icon_id): i = Icon.objects.get(id=icon_id) icons.append(i) post.icons = icons post.save() It works fine for the most part, creating a Post object and the two Icon objects. However, if the icon_id is, say, 1 in