Django 外键连接
Django Foreign Keys connections
我有一个模型 Field,它与 TreeSensor 和 WeatherStation 模型有 OneToMany 连接。我试图传递每个 treesensor/weatherstation 模型的查询,这些模型与每个不同字段的 id 相匹配,但得到一个 Field 'id' expected a number but got <built-in function id>.
。我该如何解决这个问题?也许在 filter
上更改一些内容?
class Field(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, default=None)
friendly_name = models.CharField(max_length=24, blank=True)
soil_type = models.CharField(max_length=24, choices=SOIL_TYPES, blank=True)
cultivation = models.CharField(max_length=128, choices=CULTIVATIONS, blank=True)
kml = models.FileField(upload_to = user_directory_path_kml, null=True, blank=True)
class TreeSensor(models.Model):
field = models.ForeignKey(Field, on_delete=models.CASCADE)
...
class WeatherStation(models.Model):
field = models.ForeignKey(Field, on_delete=models.CASCADE)
...
查看
def map(request):
field_list = models.Field.objects.filter(user = request.user)
tree_sensors = models.TreeSensor.objects.filter(field__pk = id)
weather_stations = models.WeatherStation.objects.filter(field__pk = id)
context = {
"title": "Map",
"field_list": field_list,
"tree_sensors": tree_sensors,
"weather_stations" : weather_stations,
}
template = 'agriculture/map.html'
return render(request, template, context)
根据您的观点,您正在过滤某些由 id
命名的字段 field
,此处未定义....
你有一个字段查询集,所以你可能应该这样做:
def map(request):
field_list = models.Field.objects.filter(user = request.user).values_list('id', flat=True)
tree_sensors = models.TreeSensor.objects.filter(field__id__in = field_list)
weather_stations = models.WeatherStation.objects.filter(field__id__in = field_list)
@Walucas 你的方法是正确的,但有点不同。结果是这样的:
查看
def map(request):
field_list = models.Field.objects.filter(user = request.user)
tree_sensors = models.TreeSensor.objects.filter(field_id__in = field_list.values_list('id',flat=True))
weather_stations = models.WeatherStation.objects.filter(field_id__in = field_list.values_list('id',flat=True))
context = {
"title": "Map",
"field_list": field_list,
"tree_sensors": tree_sensors,
"weather_stations" : weather_stations,
}
template = 'agriculture/map.html'
return render(request, template, context)
我有一个模型 Field,它与 TreeSensor 和 WeatherStation 模型有 OneToMany 连接。我试图传递每个 treesensor/weatherstation 模型的查询,这些模型与每个不同字段的 id 相匹配,但得到一个 Field 'id' expected a number but got <built-in function id>.
。我该如何解决这个问题?也许在 filter
上更改一些内容?
class Field(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, default=None)
friendly_name = models.CharField(max_length=24, blank=True)
soil_type = models.CharField(max_length=24, choices=SOIL_TYPES, blank=True)
cultivation = models.CharField(max_length=128, choices=CULTIVATIONS, blank=True)
kml = models.FileField(upload_to = user_directory_path_kml, null=True, blank=True)
class TreeSensor(models.Model):
field = models.ForeignKey(Field, on_delete=models.CASCADE)
...
class WeatherStation(models.Model):
field = models.ForeignKey(Field, on_delete=models.CASCADE)
...
查看
def map(request):
field_list = models.Field.objects.filter(user = request.user)
tree_sensors = models.TreeSensor.objects.filter(field__pk = id)
weather_stations = models.WeatherStation.objects.filter(field__pk = id)
context = {
"title": "Map",
"field_list": field_list,
"tree_sensors": tree_sensors,
"weather_stations" : weather_stations,
}
template = 'agriculture/map.html'
return render(request, template, context)
根据您的观点,您正在过滤某些由 id
命名的字段 field
,此处未定义....
你有一个字段查询集,所以你可能应该这样做:
def map(request):
field_list = models.Field.objects.filter(user = request.user).values_list('id', flat=True)
tree_sensors = models.TreeSensor.objects.filter(field__id__in = field_list)
weather_stations = models.WeatherStation.objects.filter(field__id__in = field_list)
@Walucas 你的方法是正确的,但有点不同。结果是这样的: 查看
def map(request):
field_list = models.Field.objects.filter(user = request.user)
tree_sensors = models.TreeSensor.objects.filter(field_id__in = field_list.values_list('id',flat=True))
weather_stations = models.WeatherStation.objects.filter(field_id__in = field_list.values_list('id',flat=True))
context = {
"title": "Map",
"field_list": field_list,
"tree_sensors": tree_sensors,
"weather_stations" : weather_stations,
}
template = 'agriculture/map.html'
return render(request, template, context)