允许在Python / Django中重复多对多条目的方法 [英] Way to allow for duplicate many-to-many entries in Python/Django

查看:541
本文介绍了允许在Python / Django中重复多对多条目的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Django模型:

  class Icon(models.Model):
name = models。 CharField(max_length = 200,null = False,blank = False)

class Post(models.Model):
icons = models.ManyToManyField(Icon)

当我写下面的代码:

  post = Post()
icons = []
icon_id = form.cleaned_data ['icon_1']
if(icon_id):
i = Icon.objects.get(id = icon_id)
icons.append(i)

icon_id = form.cleaned_data ['icon_2']
if(icon_id):
i = Icon.objects.get (id = icon_id)
icons.append(i)

post.icons = icons
post.save()
pre>

它大部分工作正常,创建一个Post对象和两个Icon对象。



然而,如果在两种情况下icon_id都是1,比如说,在数据库中只创建一个条目,而不是两个。



所以看起来它检查重复项并将其删除。



如何让这个工作让我允许重复? (我想要两个与一个帖子相关的SAME图标。)



谢谢!

解决方案

自己定义模型,具有这种非唯一的多对多关系

  class PostIcon 
post = models.ForeignKey(Post)
icon = models.ForeignKey(Icon)

,而不是一个一个地添加它们

 图标中的图标:
PostIcon post = post,icon = icon).save()

或将该模型传递为通过参数 ManyToManyField 例如

  class Post(models.Model):
icons = models.ManyToManyField(Icon,through = PostIcon)

,或者你可以有一个与 PostIcon 相关联的计数,而不是具有多个行,如果服务于用例,例如你可能想要一个徽章显示10次

  class PostIcon(models.Model):
post = models。 ForeignKey(Post)
icon = models.ForeignKey(Icon)
count = models.IntegerField()


I have the following Django model:

class Icon(models.Model):
    name = models.CharField(max_length=200,null=False,blank=False)

class Post(models.Model):
    icons = models.ManyToManyField(Icon)

When I write the following code:

post = Post()
icons = []
icon_id = form.cleaned_data['icon_1']
if (icon_id):
    i = Icon.objects.get(id=icon_id)
    icons.append(i)

icon_id = form.cleaned_data['icon_2']
if (icon_id):
    i = Icon.objects.get(id=icon_id)
    icons.append(i)

post.icons = icons
post.save()

It works fine for the most part, creating a Post object and the two Icon objects.

However, if the icon_id is, say, 1 in both cases, it only creates ONE entry in the database, not two.

So it seems like it checks for duplicates and removes them.

How do I make this work so I allow duplicates? (I want two of the SAME icon associated with a post.)

Thanks!

解决方案

Define the model yourself, to have such non-unique many-to-many relations

class PostIcon(models.Model):
    post = models.ForeignKey(Post)
    icon = models.ForeignKey(Icon)

and than add them one by one

for icon in icons:
    PostIcon(post=post, icon=icon).save()

or pass that model as through argument of ManyToManyField e.g.

class Post(models.Model):
    icons = models.ManyToManyField(Icon, through=PostIcon)

or alternatively you can have a count associated with PostIcon instead of having multiple rows, if that serves the use-case e.g. you may want a badge to be displayed 10 times

class PostIcon(models.Model):
    post = models.ForeignKey(Post)
    icon = models.ForeignKey(Icon)
    count =  models.IntegerField()

这篇关于允许在Python / Django中重复多对多条目的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆