添加代码后出现错误信息是Cannot resolve method 'Values()'
时间: 2023-12-30 22:02:36 浏览: 60
抱歉我的错误,`values()`应该是小写字母。请将代码中的`Values()`改为`values()`即可,代码如下所示:
```
public void loop() {
for (RealTimeData data : RealTimeData.values()) {
System.out.println(data.name() + ":" + data.getValue());
}
}
```
这样就能正确地遍历`RealTimeData`枚举类型的所有取值,并输出对应的名称和值了。
相关问题
public void copyProperties(Object source, Object target) { BeanWrapper sourceWrapper = PropertyAccessorFactory.forBeanPropertyAccess(source); BeanWrapper targetWrapper = PropertyAccessorFactory.forBeanPropertyAccess(target); PropertyValues sourceValues = sourceWrapper.getPropertyValues(); for (PropertyValue pv : sourceValues.getPropertyValues()) { String propertyName = pv.getName(); if (propertyName.startsWith(prefix)) { String targetPropertyName = propertyName.substring(prefix.length()); if (targetWrapper.isWritableProperty(targetPropertyName)) { Object value = pv.getValue(); targetWrapper.setPropertyValue(targetPropertyName, value); } }代码报错:Cannot resolve method 'getPropertyValues' in 'BeanWrapper'
这个错误提示是因为BeanWrapper类中没有getPropertyValues()方法,它被替换成了getPropertyValues(PropertyFilter filter)方法,该方法需要传入一个PropertyFilter过滤器,用于过滤掉不需要复制的属性。
因此,你可以将原来的代码:
```
PropertyValues sourceValues = sourceWrapper.getPropertyValues();
```
替换成:
```
PropertyValues sourceValues = sourceWrapper.getPropertyValues(null, true);
```
其中,第一个参数为过滤器,第二个参数表示是否包括只读属性。这样就可以解决这个报错了。
Internal Server Error: /achieve Traceback (most recent call last): File "E:\djangopro\JD\lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) File "E:\djangopro\JD\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "E:\djangopro\first\views.py", line 121, in achieve product_year = models.Order.objects.values(year=ExtractYear('orderDate'),proName = F('productName')).filter(year=ye).annotate(totalsale=Sum('productSale'), totalProfit = Sum('produ ctProfit'), File "E:\djangopro\JD\lib\site-packages\django\db\models\manager.py", line 87, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) path, final_field, targets, rest = self.names_to_path( File "E:\djangopro\JD\lib\site-packages\django\db\models\sql\query.py", line 1681, in names_to_path raise FieldError( django.core.exceptions.FieldError: Cannot resolve keyword 'productName' into field. Choices are: id, orderDate, orderId, shipDate, year
这个错误信息显示在执行 `models.Order.objects.values(year=ExtractYear('orderDate'),proName=F('productName')).filter(year=ye).annotate(totalsale=Sum('productSale'), totalProfit=Sum('productProfit'))` 时出现了问题。
从错误信息来看,Django 无法将 `productName` 解析为一个有效的字段名,因为该字段名在你的 `Order` 模型中不存在。错误信息中也提供了一些可用的字段名,包括 `id`、`orderDate`、`orderId`、`shipDate` 和 `year`,你需要检查一下你的 `Order` 模型中是否存在名为 `productName` 的字段。
如果你的 `Order` 模型中确实存在名为 `productName` 的字段,那么你需要检查一下该字段名是否正确拼写,或者是否使用了正确的大小写。你还可以尝试使用 `__` 来访问该字段的关联模型,例如 `product__name`。
如果你的 `Order` 模型中确实不存在名为 `productName` 的字段,那么你需要检查一下你的查询是否正确,在查询中使用了正确的字段名。你还可以检查一下你的模型定义,确保你的模型中包含了你需要的字段。
阅读全文