Django如何从查询器向对象添加数据 [英] Django how to add data to Object from queryset

查看:118
本文介绍了Django如何从查询器向对象添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示客户端列表,并显示分配给他们的标签,但我有问题,因为我的标签在其他表中,我不知道如何连接数据在一起。客户端可以有几个标签,也可以没有。这样做是正确的?你能给我一些建议吗我需要一个变量,其中带有逗号分隔的逗号或带有标签的对象,我可以在模板引擎中使用。



Views.py:

  @user_passes_test(lambda u:u.is_staff,login_url ='/ account / login /')
def client_list(request)
dict = {}
dict ['clients'] = Client.objects.all()

return render(request,'panel / client / list.html',dict )

Models.py:

  class Client(models.Model):
id = models.OneToOneField(User,on_delete = models.CASCADE,unique = True,primary_key = True)
uuid = models.UUIDField(default = uuid.uuid4,editable = False)
name = models.CharField(max_length = 256,unique = True)

class TagsClientChoices(models。模型):
name = models.CharField(max_length = 80,unique = True)

class TagsClientList(models.Model):
tag_id = models.ForeignKey('TagsClientChoices' )
cl ient = models.ForeignKey('Client',blank = True,null = True)


解决方案

默认情况下,每个外键与_set



有反向关系,所以您可以执行



<$客户端的客户端的p $ p>
client.tagsclientlist_set.all()中的标签列表:
#使用标签列表

尽管这可能不是很好,但您可能希望 prefetch_related ,并提供一个相关的名称,它将以两个查询而不是多个查询来检索结果。 p>

  class TagsClientList(models.Model):
tag_id = models.ForeignKey('TagsClientChoices')
client = models.ForeignKey('Client',blank = True,null = True,related_name ='tags_list')



dict ['clients'] = Client.objects.all ().prefetch_related('标签_list')

客户端中的客户端:
在client.tags_list.all()中的标签列表:
#使用标签列表
/ pre>

I would like show list of clients and show tags assigned to them but I have problem because I have my tags in other table and I dont know how to connect data together. Clients can have couple of tags or none of that. Which way is correct to do this? Can you advice me something? I need one variable with tags separated comma or objects with tags which I can use in template engine.

Views.py:

@user_passes_test(lambda u: u.is_staff, login_url='/account/login/')
def client_list(request):
    dict = {}
    dict['clients'] = Client.objects.all()

    return render(request, 'panel/client/list.html', dict)

Models.py:

class Client(models.Model):
    id = models.OneToOneField(User, on_delete=models.CASCADE, unique=True, primary_key=True)
    uuid = models.UUIDField(default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=256, unique=True)

class TagsClientChoices(models.Model):
    name = models.CharField(max_length=80, unique=True)

class TagsClientList(models.Model):
    tag_id = models.ForeignKey('TagsClientChoices')
    client = models.ForeignKey('Client', blank=True, null=True)

解决方案

By default every foreignkey has a reverse relation with _set

so you could do

for client in clients:
    for taglist in client.tagsclientlist_set.all():
        # use taglist

Although this may not be very performant, you may want to prefetch_related and provide a related_name which will retrieve the results in two queries rather than multiple.

class TagsClientList(models.Model):
    tag_id = models.ForeignKey('TagsClientChoices')
    client = models.ForeignKey('Client', blank=True, null=True, related_name='tags_list')



dict['clients'] = Client.objects.all().prefetch_related('tags_list')

for client in clients:
    for taglist in client.tags_list.all():
        # use taglist

这篇关于Django如何从查询器向对象添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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