我的应用程序中有这样的模型:
class User(AbstractUser):
pass
class MyObject(models.Model):
name = models.CharField(max_length=200, unique=False, db_index=True)
related_users = models.ManyToManyField(
User, through='RelatedUsers', related_name='related_users'
)
class RelatedUsers(models.Model):
my_object = models.ForeignKey(
MyObject, related_name='my_object_related_users'
)
user = models.ForeignKey(User)
type = models.CharField(
max_length=100,
choices=RelatedUsersTypes.choices()
)
class Meta:
unique_together = ('user', 'my_object', 'type')
class FunctionalityRelatedUsersTypes(BaseChoiceEnum):
TYPE_1 = 'TYPE 1'
TYPE_2 = 'TYPE 2'
TYPE_3 = 'TYPE 3'
TYPE_4 = 'TYPE 4'
TYPE_5 = 'TYPE 5'
我想知道是否可以在MyObject
上创建某种合成关系。
我希望能够使用一个字段(例如:related_users_type_1
)按类型吸引用户。我也想在DRF序列化器中使用它(因此我可以只传递ID列表,而Relation将创建具有相应类型的Proxy对象)。
伪代码:
related_users_type_1 = models.RelationField(RelatedUsers, filter={'type': 'TYPE_1'})
我想发送的样本有效载荷:
{
"related_users_type_1": [1, 2, 3],
"related_users_type_2": [3]
}
预期结果:
RelatedUsers
与TYPE_1
RelatedUser
和TYPE_2
答案 0 :(得分:0)
根据我们已有的评论,我认为您的问题可以通过以下模型来解决:
@FrozenValue
@Column
private Map<String, Map<String,Integer>> assessmentMap;
django documentation向您展示了如何遍历关系。
使用# just for completeness. should probably use an actual user model
class User (models.Model):
name = models.CharField(max_length=64)
class MyObject(models.Model):
name = models.CharField(max_length=64)
users = models.ManyToMany(User, through='MyObjectUsers', symmetrical=False)
def add_relations(self, relations):
for key, list in relations.items():
for id in list:
users.add(user=User.objects.get(pk=id), rel_type=key)
class MyObjectUsers(models.Model):
REL_TYPE_CHOICES=(
('TYPE1','TYPE1'),
('TYPE2','TYPE2')
)
myobject = models.ForeignKey('MyObject', null=True, on_delete=models.SET_NULL)
user = models.ForeignKey('User', null=True, on_delete=models.SET_NULL)
rel_type = models.CharField(max_length=32, choices = REL_TYPE_CHOICES)
的上述实现,您应该能够在MyObject
实例上调用.add_relations({'TYPE1':[1,2,3]})
来创建所需的关系。
我在手机上写了这个,所以我真的不知道它是否可以运行(明天会测试)