我应该怎么做才能使用映射 filter() 函数结果的字典?
What should I do so that I can use a dictionary mapping the filter() function result?
我有这样一个模型:
class BNetworkEdge(models.Model):
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField()
class Meta:
managed = True
db_table = 'b_network_edge'
我有这样的字典:
{0:'direct', 1:'no-direct'}
我运行这个在pythonshell
>>> BNetworkEdge.objects.filter()[0].edge_type
0
如何得到 'direct' 而不是 '0' 的结果?
谢谢!
为了在您的视图中获得字典映射结果,您将执行如下操作:
#Your models.py
class BNetworkEdgeManager(models.Manager):
def get_queryset(self):
# The return of query by manager
return list(super().get_queryset().values('id', 'data_dict'))
class BNetworkEdge(models.Model):
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField()
objects = BNetworkEdgeManager()
# This goes in views.py
from .models import BNetworkEdge
def bnetwork_edge_view(request):
# this context to holds your data as a dictionary
context = {}
dict_data = BNetworkEdge.objects.all()
context = {'dict_data':dict_data}
# You can print the content of your context variable
# to see the dictionary objects
print(context)
return render(request,"Put Your Html Template Here eg.('network.html/')",context)
另外,要将 edge_type 的值作为字典获取,您需要创建一个方法来将 edge_type 句柄作为字典获取,并将它们 link 与属性 装饰器!因此:
class BNetworkEdge(models.Model):
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField()
objects = BNetworkEdgeManager()
@property
def get_edge_type(self):
dict_values = {0:'dict_data', 1:'data'}
return dict_values[self.edge_type]
# Then you call this in your terminal:
BNetworkEdge.objects.filter()[0].get_edge_type()
#which returns:
[output] {0: 'dict_data'}
您可以为此创建一个 property 作为,
@property
def edge_type_value(self):
_vals = {0:'direct', 1:'no-direct'}
return _vals[self.edge_type]
你的代码就像,
from django.db import models
class BNetworkEdge(models.Model):
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField()
class Meta:
managed = True
db_table = 'b_network_edge'
@property
def edge_type_value(self):
_vals = {0:'direct', 1:'no-direct'}
return _vals[self.edge_type]
您可以访问它,
In [10]: BNetworkEdge.objects.filter()[0].edge_type_value
Out[10]: 'direct'
在您的字段中添加适当的选项choices
class BNetworkEdge(models.Model):
class EdgeTypes(models.IntegerChoices):
DIRECT = 1, 'direct'
NO_DIRECT = 2, 'no-direct'
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField(choices=EdgeTypes.choices)
class Meta:
managed = True
db_table = 'b_network_edge'
然后您可以调用 get_edge_type_display
来获取选择标签
BNetworkEdge.objects.filter()[0].get_edge_type_display()
我有这样一个模型:
class BNetworkEdge(models.Model):
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField()
class Meta:
managed = True
db_table = 'b_network_edge'
我有这样的字典:
{0:'direct', 1:'no-direct'}
我运行这个在pythonshell
>>> BNetworkEdge.objects.filter()[0].edge_type
0
如何得到 'direct' 而不是 '0' 的结果?
谢谢!
为了在您的视图中获得字典映射结果,您将执行如下操作:
#Your models.py
class BNetworkEdgeManager(models.Manager):
def get_queryset(self):
# The return of query by manager
return list(super().get_queryset().values('id', 'data_dict'))
class BNetworkEdge(models.Model):
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField()
objects = BNetworkEdgeManager()
# This goes in views.py
from .models import BNetworkEdge
def bnetwork_edge_view(request):
# this context to holds your data as a dictionary
context = {}
dict_data = BNetworkEdge.objects.all()
context = {'dict_data':dict_data}
# You can print the content of your context variable
# to see the dictionary objects
print(context)
return render(request,"Put Your Html Template Here eg.('network.html/')",context)
另外,要将 edge_type 的值作为字典获取,您需要创建一个方法来将 edge_type 句柄作为字典获取,并将它们 link 与属性 装饰器!因此:
class BNetworkEdge(models.Model):
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField()
objects = BNetworkEdgeManager()
@property
def get_edge_type(self):
dict_values = {0:'dict_data', 1:'data'}
return dict_values[self.edge_type]
# Then you call this in your terminal:
BNetworkEdge.objects.filter()[0].get_edge_type()
#which returns:
[output] {0: 'dict_data'}
您可以为此创建一个 property 作为,
@property
def edge_type_value(self):
_vals = {0:'direct', 1:'no-direct'}
return _vals[self.edge_type]
你的代码就像,
from django.db import models
class BNetworkEdge(models.Model):
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField()
class Meta:
managed = True
db_table = 'b_network_edge'
@property
def edge_type_value(self):
_vals = {0:'direct', 1:'no-direct'}
return _vals[self.edge_type]
您可以访问它,
In [10]: BNetworkEdge.objects.filter()[0].edge_type_value
Out[10]: 'direct'
在您的字段中添加适当的选项choices
class BNetworkEdge(models.Model):
class EdgeTypes(models.IntegerChoices):
DIRECT = 1, 'direct'
NO_DIRECT = 2, 'no-direct'
id = models.AutoField(unique=True, primary_key=True)
source = models.IntegerField()
target = models.IntegerField()
edge_type = models.IntegerField(choices=EdgeTypes.choices)
class Meta:
managed = True
db_table = 'b_network_edge'
然后您可以调用 get_edge_type_display
来获取选择标签
BNetworkEdge.objects.filter()[0].get_edge_type_display()