m2m

Django M2M Through extra fields with multiple models

天大地大妈咪最大 提交于 2019-12-12 06:18:10
问题 I'm trying to figure out the best way to set up the following django model (genericised for security reasons). ThingA: User(M2M through "UserRelation") ThingB: User(M2M through "UserRelation") ThingC: User(M2M through "UserRelation") User: Login_name UserRelation: User (foreginkey) Thing (foreignkey) #is this generic to any of the above "things" Privilege I understand using "through" between two distinct models, but I'm not sure how to apply this to multiple models. Would I define a

Django: how to execute code ONLY after the first time a M2M relationship is added?

一个人想着一个人 提交于 2019-12-11 19:00:16
问题 I'm trying to get create_reminder_send_message() executed THE FIRST TIME the Reminder object is saved AND the Reminder.users is saved. The code as it is executes every time I update the object... what am I missing? How can I accomplish what I want? class Reminder(models.Model): METHODS = ( ('EM', 'Send Email'), ('TS', 'Create Dashboard Task'), ('ET', 'Both (recommended)') ) info = models.TextField() method = models.CharField(max_length=3, choices=METHODS, db_index=True, help_text='''How

Insert for model with m2m in beego orm

℡╲_俬逩灬. 提交于 2019-12-11 06:36:49
问题 I have two models: type MainFields struct { Id int `orm:"auto"` Created time.Time `orm:"auto_now_add;type(datetime)"` Updated time.Time `orm:"auto_now;type(datetime)"` } type Game struct { MainFields Players []*Player `orm:"rel(m2m)"` } type Player struct { MainFields Games []*Game `orm:"reverse(many)"` NickName string } And with this code i`am trying to create new game with one player: func insertTestData() { var playerA models.Player playerA.NickName = "CoolDude" id, err := models.ORM

Many-to-many relation on User table in Django

我与影子孤独终老i 提交于 2019-12-08 04:40:49
问题 I'm writing an app where I need to associate data with user pairs. For instance, each user pair will have a compatibility score associated with them, as well as many-to-many relationships such as artists that they have in common. I'm confused about the best way to do this, it seems like I would use a combination of 1) extending User via the one-to-one relationship, 2) using a recursive relationship to self on the User table, 3) coupled with specifying extra fields on M2M relationships, but I

Editing both sides of M2M in Admin Page

吃可爱长大的小学妹 提交于 2019-12-07 01:40:43
问题 First I'll lay out what I'm trying to achieve in case there's a different way to go about it! I want to be able to edit both sides of an M2M relationship (preferably on the admin page although if needs be it could be on a normal page) using any of the multi select interfaces. The problem obviously comes with the reverse side, as the main side (where the relationship is defined) works just fine automagically. I have tried some of the advice here to get an inline to appear and that works but

Django, in many to many relationship within the self class, how do I reference each other in terms of ORM?

回眸只為那壹抹淺笑 提交于 2019-12-05 20:44:51
class User(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() gender = models.IntegerField() email = models.CharField(max_length=100) password = models.CharField(max_length=255) following = models.ManyToManyField("self", related_name='followers') objects = UserManager() def __repr__(self): return "User: {0}".format(self.name) In my model, User, users can follow and followed by each other. I can find who the user is following by this: user1 = User.objects.get(id=1) following = user1.following.all() However, I can't figure out how to find whom the user is followed

Editing both sides of M2M in Admin Page

假装没事ソ 提交于 2019-12-05 04:33:04
First I'll lay out what I'm trying to achieve in case there's a different way to go about it! I want to be able to edit both sides of an M2M relationship (preferably on the admin page although if needs be it could be on a normal page) using any of the multi select interfaces. The problem obviously comes with the reverse side, as the main side (where the relationship is defined) works just fine automagically. I have tried some of the advice here to get an inline to appear and that works but its not a very nice interface. The advice I got on the django mailing list was to use a custom ModelForm.

ManyToMany field not saved when using Django admin

泪湿孤枕 提交于 2019-12-05 01:05:36
I'm experiencing a weird problem which I hope someone in here may be able to shed some light on. I'm overriding the save() method of a model to add some values to a ManyToMany-field after running super(). My problem is that when I'm saving in Django admin the values seems to get added to the relationship but is then empty again. If however I do it from manage.py shell it works without problem. I've put two print statements in there and they produce the exact same output regardless of if I'm running it via Django admin or via shell. class Store(models.Model): holidays = models.ManyToManyField

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

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