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 foreignkey for each of the "Thing" models in my UserRelation Model?


回答1:


It looks like you are trying to setup a generic many-to-many relationship. There is a dedicated django app that you can be use for this purpose: django-gm2m

Here is how to use it in your generic case:

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey

from gm2m import GM2MField


class ThingA(models.Model):
    pass

class ThingB(models.Model):
    pass

class ThingC(models.Model):
    pass

class User(models.Model):
     login_name = models.CharField(max_length=255)
     things = GM2MField(through='UserRelation')

class UserRelation(models.Model):
     user = models.ForeignKey(User)
     thing = GenericForeignKey(ct_field='thing_ct', fk_field='thing_fk')
     thing_ct = models.ForeignKey(ContentType)
     thing_fk = models.CharField(max_length=255)

     privilege = models.CharField(max_length=1)

You can now access all the things for a given user and all the User instances for a given 'thing', as well as the privilege attribute for each UserRelation instance.

This will additionally provide you with a handful of benefits (reverse relations, prefetching, etc.) you may need. A GM2MField basically behaves exactly like a django ManyToManyField.

Disclaimer: I am the author of django-gm2m



来源:https://stackoverflow.com/questions/29353833/django-m2m-through-extra-fields-with-multiple-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!