many-to-many

Compound/Composite primary/unique key with Django

爱⌒轻易说出口 提交于 2019-12-06 18:45:02
问题 How can you create models (and thus tables) with a compound (composite) primary/unique key using Django? 回答1: Django does not support compound primary keys. You can create a single compound unique key with Meta.unique_together. 回答2: if you want only unique mixed fields together use belowcode: class MyTable(models.Model): class Meta: unique_together = (('key1', 'key2'),) key1 = models.IntegerField() key2 = models.IntegerField() But if you want unique together and one of column be primary, set

How to query for all groups of type 'foo' that contain user_x? (many-to-many table)

爷,独闯天下 提交于 2019-12-06 17:12:11
问题 Given the following tables below, how do I query for all groups of grouptype 'foo' for user_x ? The equivalent SQL would be something like: SELECT * FROM users_to_groups LEFT JOIN users ON user_id=users.id LEFT JOIN groups ON group_id=groups.id WHERE groups.type='type1' AND user_id=1; I was thinking the SQLAlchemy query would look something like: session.query(UserGroup).filter(UserGroup.user==user_x, UserGroup.group.grouptype=='foo') but I don't know how to specify the grouptype (the above

Django Grappelli Autocomplete M2M

☆樱花仙子☆ 提交于 2019-12-06 15:44:32
I followed the docs down to the letter and can't get the M2M autocomplete lookup to work in Grappelli. #models.py #main model class Entry(models.Model): title = models.CharField(max_length=60) content = models.TextField() keywords = models.ManyToManyField(Keyword, blank=True) #model I want to be searched through while typing in the autocomplete field class Keyword(models.Model): name = models.CharField(max_length=30) @staticmethod def autocomplete_search_field(): return ('id__iexact', 'name__icontains',) def __str__(self): return '%s' % (self.name) then in admin.py : class EntryAdmin(admin

Many-to-Many relationship only works one way Rails / Activerecord / devise

时间秒杀一切 提交于 2019-12-06 15:43:49
I have a many-to-many relationship User to Lists User model was created with devise. For some reason I cant access .lists on any of the users. returns undefined method `to_sym' for nil:NilClass. after doing some digging im pretty sure ive found the problem in _reflect_on_associationactiverecord (4.1.6) lib/active_record/reflection.rb def _reflect_on_association(association) #:nodoc: _reflections[association.to_sym] end association is being passed in as nil. Has anyone run into this problem before? Testing relationship in rails console [15] pry(main)> testuser = User.new => #<User id: nil,

DJANGO: How to Output CSV with model containing many-to-many field?

梦想的初衷 提交于 2019-12-06 14:58:50
I have an "Export to CSV" button that exports all car models. When I export all cars into CSV, the "features" (e.g. AM/FM Radio, Moon Roof, Leather Interior, Bluetooth, GPS etc..) column displays as the following: [<Feature: GPS>, <Feature: Leather>] How do I get rid of all that other stuff, and just have "GPS, Leather"? MODEL class Features(models.Model): name = models.CharField(max_length=20) def __unicode__(self): return self.name class Car(models.Model): model_name = models.CharField(max_length=20) features = models.ManyToManyField(features) def __unicode__(self): return self.model_name

JPA @manytomany unidirectional mapping

我的梦境 提交于 2019-12-06 14:58:38
When I try remove ExternalDataStorage This query throw a constraint violation exception with constraint name null .ExternalDataStorage has no relation mapping on it.Query first check ExternalTasks whichs contain ExternalDataStorage(Which will be removed) in ExternalTask's externalsources List and remove from list, if list is empty remove externaltask and finally remove ExternalDataStorage entity(Target entity). public boolean deleteExternalDataStorage(Long sid) { EntityManager em = getEntityManager(); EntityTransaction et = em.getTransaction(); try { et.begin(); ExternalDataStorage s = em.find

MongoDB : where is the limit between “few” and “many”?

拥有回忆 提交于 2019-12-06 14:47:10
I am coming from the relational database world (Rails / PostgreSQL) and transitioning to the NoSQL world (Meteor / MongoDB), so I am learning about denormalization, embedding and true links. It seems that, in many cases, choosing between various database schemas comes down to the number of documents that will be "related" to each others. In this video series , the author distinguishes: one-to-many relationships from one-to-few relationships many-to-many relationships from few-to-few relationships So, I am wondering: where is the limit between few and many ? I guess there may not be a hard

Sql Select phone numbers from a many to many table with different types (mobile, home)

蹲街弑〆低调 提交于 2019-12-06 13:38:38
I have a simple table schema: Person: ID, Name PhoneNumber: ID, Type, Number #Type can be 'home' or 'mobile'. PersonPhoneNumber: ID, Person_ID, PhoneNumber_ID #A join table that connects the #person to a phone number. As data I have: Person: 1, "Ed" PhoneNumber: 1, "home", 1111 PhoneNumber: 2, "mobile", 2222 PersonPhoneNumber: 1, 1 /*(Person_ID)*/, 1 /*(PhoneNumber_ID*/ PersonPhoneNumber: 2, 1 /*(Person_ID)*/, 2 /*(PhoneNumber_ID*/ I want to write a view that returns: Name |Home |Mobile ----------------------------- "Ed" 1111 2222 "Joe" 3333 4444 ... etc Any tips on how I approach this? Note:

Delete an item from many-to-many relationship

纵然是瞬间 提交于 2019-12-06 13:02:36
I've following mapping for two tables having a Many-to-Many relationship between them. How do I delete an entry from the mapping table, which is 'TB_EMAIL_GRUPO_ARQUIVO' in my case? I just need to delete the data from this "joining" table and I don´t need to delete it from the "parent tables". GrupoModulo public GrupoModuloMap() { Schema(Const.SCHEMA); Table(Const.TB_EMAIL_GRUPO_MODULO); CompositeId() .KeyReference(x => x.Grupo, Const.ID_GRUPO) .KeyReference(x => x.Modulo, Const.ID_MODULO); Map(x => x.GrupoId).Column(Const.ID_GRUPO).ReadOnly(); Map(x => x.ModuloId).Column(Const.ID_MODULO)

How to convert django ManyToManyField into Django-nonrel Field?

怎甘沉沦 提交于 2019-12-06 11:52:25
I build an app in django, but since I found out that google app engine doesn't support Django out of the box (free,cloud sql can't be used for free right?). I decided to move to Django-nonrel, so there are few datebase Field that need converting, and I don't know how: class Cate(models.Model): name = models.CharField(max_length = 100) description = models.TextField() create_by = models.ForeignKey(User) create_date = models.DateTimeField('cate created date') def __unicode__(self): return self.name class Product(models.Model): product_name = models.CharField(max_length = 200) owner = models