我想知道如何将查询集保存在数据库中
def data_min(self):
qs1 = Csr.objects.values_list('value_base')
qs2 = Modelb.objects.filter(Q(limit_a__gte=qs1) & (Q(limit_b__lte=qs1))).distinct().values_list('limit_b')
qs3 = qs2[0]
return qs3
此函数返回我:
<QuerySet [(Decimal('133.22'),)]>
现在我的问题是如何将其保存在“ bdata_superior”字段中的模型中:
class Csr(models.Model):
tarifa_cobro = models.ForeignKey(Tarifa_Sem, on_delete=models.CASCADE, null=True, blank=True)
value_base = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
bdata_superior = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
data_inferior = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
class Modelb(models.Model):
limit_a = models.DecimalField(max_digits=10, decimal_places=2)
limit_b = models.DecimalField(max_digits=10, decimal_places=2)
问题是,当我保存它时,出现以下错误:
conversion from method to Decimal is not supported
def save(self, *args, **kwargs):
self.bdata_superior = self.data_min
super (Crs, self).save(*args, **kwargs)
感谢支持
答案 0 :(得分:0)
调用data_min
函数,然后将返回值保存到self.bdata_superior
,您当前正在将函数分配给self.bdata_superior
def save(self, *args, **kwargs):
self.bdata_superior = self.data_min()
super (Crs, self).save(*args, **kwargs)
答案 1 :(得分:0)
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/cisr/Csr/1/change/
Django Version: 2.2.1
Python Version: 3.7.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cisr',
'import_export']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\contrib\admin\options.py" in wrapper
606. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\utils\decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\contrib\admin\sites.py" in inner
223. return view(request, *args, **kwargs)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\contrib\admin\options.py" in change_view
1637. return self.changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\utils\decorators.py" in _wrapper
45. return bound_method(*args, **kwargs)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\utils\decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\contrib\admin\options.py" in changeform_view
1522. return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\contrib\admin\options.py" in _changeform_view
1561. self.save_model(request, new_object, form, not add)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\contrib\admin\options.py" in save_model
1088. obj.save()
File "C:\Users\ALFREDO\CISR\cisr\models.py" in save
42. super (Calculadora_isr, self).save(*args, **kwargs)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\base.py" in save
741. force_update=force_update, update_fields=update_fields)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\base.py" in save_base
779. force_update, using, update_fields,
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\base.py" in _save_table
851. forced_update)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\base.py" in _do_update
900. return filtered._update(values) > 0
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\query.py" in _update
760. return query.get_compiler(self.db).execute_sql(CURSOR)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1429. cursor = super().execute_sql(result_type)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1087. sql, params = self.as_sql()
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
1395. val = field.get_db_prep_save(val, connection=self.connection)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
1563. return connection.ops.adapt_decimalfield_value(self.to_python(value), self.max_digits, self.decimal_places)
File "C:\Users\ALFREDO\CISR\env\lib\site-packages\django\db\models\fields\__init__.py" in to_python
1554. return decimal.Decimal(value)
Exception Type: ValueError at /admin/cisr/Csr/1/change/
Exception Value: argument must be a sequence of length 3
答案 2 :(得分:0)
我更新对此线程的响应。
首先更新我的qs2,该错误向我显示,因为我试图保存queryset,然后保存一个元组,以访问我的queryset的值,只需在values_list()方法内添加flat = True参数,然后访问我的元组的第一个值,位置为[0]
这是我更新的功能
canvas.loadFromJSON()
和保存方法
def data_min(self):
qs1 = Csr.objects.values_list('base_gravada')
qs2 = Modelb.objects.filter
(Q(limit_a__gte=qs1) &
Q(limit_b__lte=qs1))
.distinct().values_list('limit_b', flat=True)[0]
非常感谢您@ harshil9968