在Django ORM多对多关系中为对象分配标记

时间:2022-10-04 19:23:22

I have three tables. Tag, QueryStringTag, and QueryString. QueryStringTag a third table that is created to connect Tag and QueryString and allow one Tag to be associated with many QueryString's and vice versa. It contains two foreign keys. One pointed at Tag and one pointed at QueryString.

我有三个表。标签,QueryStringTag和变量的名称。QueryStringTag第三个表,用于连接标记和QueryString,并允许一个标记与许多QueryString关联,反之亦然。它包含两个外键。一个指向标记,一个指向QueryString。

The flow goes like this. A QueryString object is created and saved in the database. After this, 4 Tags are created. I want to associate those takes with the particular QueryString object.

流动是这样的。在数据库中创建并保存了一个QueryString对象。在此之后,将创建4个标记。我想将这些获取与特定的QueryString对象关联起来。

I have failed at all attempts so far. The docs are not providing much help and I have tried just about everything I can think of.

到目前为止,我所有的尝试都失败了。医生们并没有提供多少帮助,我已经尝试了我能想到的一切。

Here are my models...

下面是我的模型…

class QueryString(BaseObject): 

    server_id = models.IntegerField()
    schema = models.CharField(max_length=255, blank=True)
    query = models.CharField(max_length=60000) 
    variables = models.TextField(blank=True)
    created_by = models.ForeignKey(User, related_name='queries_created')
    updated_by = models.ForeignKey(User, related_name='queries_last_edited')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField()
    touched_by = models.CharField(max_length=1000)
    config = models.TextField(blank=True)
    runs_started = models.IntegerField(default=0)
    runs_completed = models.IntegerField(default=0)
    runs_completed_duration = models.IntegerField(default=0) # total number of seconds spent running this query to completion
    formats = "pretty_html html json prettyjson csv excel tableau".split()

class Tag(models.Model):
    name = models.CharField(max_length=100)

class QueryStringTab(models.Model):
    tag = models.ForeignKey(Tag, related_name='querystringtag')
    querystring = models.ForeignKey(QueryString, related_name='querystringtag')

Here is my view...
(Keep in mind that query is a unique QueryString object that I am trying to associate with the four tags below)

这是我的看法……(请记住,查询是一个惟一的QueryString对象,我正在尝试将其与下面的四个标记关联)

tags = ['one', 'two', 'three', 'four']

for tag in tags:
    newtag = Tag(name=tag)
    newtag.save()
    query.querystringtab.tag(newtag.pk)

Can anyone point me in the right direction?

谁能给我指出正确的方向吗?

1 个解决方案

#1


1  

I'd suggest searching about here on *. I found two probably relevant answers searching "python django many to many":

我建议在*上搜索一下。我在“python django many to many”中找到了两个可能相关的答案:

For example: Way to allow for duplicate many-to-many entries in Python/Django

例如:允许在Python/Django中重复多对多条目的方法

#1


1  

I'd suggest searching about here on *. I found two probably relevant answers searching "python django many to many":

我建议在*上搜索一下。我在“python django many to many”中找到了两个可能相关的答案:

For example: Way to allow for duplicate many-to-many entries in Python/Django

例如:允许在Python/Django中重复多对多条目的方法