Django - How to save m2m data via post_save signal?

前端 未结 3 1713
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 08:08

(Django 1.1) I have a Project model that keeps track of its members using a m2m field. It looks like this:

class Project(models.Model):
    members = models         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 09:00

    I can't see anything wrong with your code, but I'm confused as to why you think the admin should work any different from any other app.

    However, I must say I think your model structure is wrong. I think you need to get rid of all those ForeignKey fields, and just have a ManyToMany - but use a through table to keep track of the roles.

    class Project(models.Model):
        members = models.ManyToManyField(User, through='ProjectRole')
    
    class ProjectRole(models.Model):
        ROLES = (
           ('SR', 'Sales Rep'),
           ('SM', 'Sales Manager'),
           ('PM', 'Project Manager'),
        )
        project = models.ForeignKey(Project)
        user = models.ForeignKey(User)
        role = models.CharField(max_length=2, choices=ROLES)
    

提交回复
热议问题