many-to-many

Django: accessing ManyToManyField objects after the save

浪子不回头ぞ 提交于 2019-12-03 21:11:22
This is baffling me... When I save my model, the book objects are unchanged. But if I open the invoice and save it again, the changes are made. What am I doing wrong? class Invoice(models.Model): ... books = models.ManyToManyField(Book,blank=True,null=True) ... def save(self, *args, **kwargs): super(Invoice, self).save(*args, **kwargs) for book in self.books.all(): book.quantity -= 1 if book.quantity == 0: book.sold = True; book.save() Edit: I've tried using the post_save signal, but it works the same way. No changes on the first save, changes saved the second time. Update: Seems to be solved

Many-to-Many with association object and all relationships defined crashes on delete

巧了我就是萌 提交于 2019-12-03 20:59:47
When having a fully fledged many-to-many with all relations described, the deletion of one of the two main objects crashes. Description Car ( .car_ownerships ) <-> ( .car ) CarOwnership ( .person ) <-> ( .car_ownerships ) Person Car ( .people ) <-----------------> ( .cars ) Person Problem When deleting a Car or a Person SA first deletes the association object CarOwnership (because of the 'through' relationship with the secondary argument) and then tries to update the foreign keys to NULL in the same association objects, hence crashing. How should I solve this? I'm a little perplexed to see

How can I build/create a many-to-many association in factory_girl?

我们两清 提交于 2019-12-03 20:34:48
I have a Person model that has a many-to-many relationship with an Email model and I want to create a factory that lets me generate a first and last name for the person (this is already done) and create an email address that is based off of that person's name. Here is what I have for create a person 's name: Factory.sequence :first_name do |n| first_name = %w[FirstName1 FirstName2] # ... etc (I'm using a real subset of first names) first_name[(rand * first_name.length)] end Factory.sequence :last_name do |n| last_name = %w[LastName1 LastName2] # ... etc (I'm using a real subset of last names)

EF repository pattern many to many insert

一曲冷凌霜 提交于 2019-12-03 20:24:36
We have 2 tables: Table Authority: public class Authority { public int ID {get;set;} public string Name{get;set;} ... } Table Agents public class Agent { public int ID{get;set;} public int FirstName{get;set;} } And we have a relationship many-to-many between these two tables: public class AuthorityConfiguration : EntityTypeConfiguration<Authority> { public AuthorityConfiguration() : base() { HasKey(p => p.ID); HasMany(p => p.Agents).WithMany(a => a.Authorities).Map(mc => { mc.MapLeftKey("AuthorityID"); mc.MapRightKey("AgentID"); mc.ToTable("AuthorityAgent"); }); ToTable("Authority"); } }

Many-to-Many Nested Attributes in Rails 4 (with strong parameters)

泄露秘密 提交于 2019-12-03 20:09:11
I have been trying to figure this one out for a few days now. I am using Rails 4 (with the updated mass assignment technique) and trying to use nested attributes with a many-to-many relationship. My record is saving to the DB but everything is nil and I'm getting an "Unpermitted parameters: school, alumnis, prospects" error in my logs. Here's what I have: referral.rb class Referral < ActiveRecord::Base belongs_to :school belongs_to :alumni belongs_to :prospect end alumni.rb class Alumni < ActiveRecord::Base has_many :referrals has_many :prospects, through: :referrals accepts_nested_attributes

@ManyToMany(mappedBy = “foo”)

大城市里の小女人 提交于 2019-12-03 18:30:14
问题 Foo has: @ManyToMany(mappedBy = "foos") private Set<Bar> bars and Bar has : @ManyToMany private Set<Foo> foos What difference does the location of mappedBy attribute make to a bi-directional relationship , other than whether table is called foo_bar, or bar_foo; and without the mappedBy attribute I get two join tables, both foo_bar and bar_foo. 回答1: The documentation says: If the association is bidirectional, one side has to be the owner and one side has to be the inverse end (ie. it will be

Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used?

纵饮孤独 提交于 2019-12-03 17:30:36
问题 An example Many-to-Many through relationship in Django: class First(models.Model): seconds = models.ManyToManyField(Second, through='Middle') class Middle(models.Model): first = models.ForeignKey(First) second = models.ForeignKey(Second) class Second(models.Model): Following the documentation on intermediary models, only one model of the pair to be related contains the ManytoManyField , model First in the example above. Is this correct? If so, which model should contain the ManytoManyField

many to many table with an extra column in Rails

微笑、不失礼 提交于 2019-12-03 16:49:23
Hi guys! Is possible to do this with only two Rails models, User and Event: Users |id |name |age | |1 |danilo |26 | |2 |joe |23 | |3 |carlos |50 | |4 |katy |45 | Events_Users |event_id |user_id |confirmed | |1 |1 |1 | |3 |3 |0 | |4 |3 |1 | |2 |3 |1 | Events |id |name |date | |1 |the end of the year |31/12/2012 | |2 |the end of the world |21/12/2012 | |3 |Party |18/12/2012 | |4 |Dinner |19/12/2012 | The problem is, the user can confirm or not their presence in an event, for this I used the table Events_Users, column confirmed (1 for confirmed). How can I do this with Rails ActiveRecord without

Many-to-many to DTOs using Automapper

匆匆过客 提交于 2019-12-03 16:37:27
If I have a many-to-many relationship defined in EF: public class StudentImage { public int StudentId { get; set; } public int ImageId { get; set; } public int Order { get; set; } public virtual Student Student { get; set; } public virtual Image Image { get; set; } } public class Student { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<StudentImage> Images { get; set; } } public class Image { public int Id { get; set; } public string Filename { get; set; } public virtual ICollection<StudentImage> Students { get; set; } } And the DTO's: public class

How do I handle table relationships with the repository pattern?

◇◆丶佛笑我妖孽 提交于 2019-12-03 16:34:08
问题 I'm implementing the repository pattern as part of an ASP.NET MVC site. Most examples I've seen of repositories are fairly simple. For example here's a typical abstract repository interface. public interface IRepository<TEntity> { IQueryable<TEntity> All(); TEntity FindBy(int id); TEntity FindBy(Expression<Func<TEntity, bool>> expression); IQueryable<TEntity> FilterBy(Expression<Func<TEntity, bool>> expression); bool Add(TEntity entity); bool Update(TEntity entity); bool Delete(TEntity entity