This is a common error on stack overflow but I cannot draw similarities with their errors and mine.
A number is expected in the id column of the database(automatically made by django) but a query dict was put in instead.
(These are mode details so that I can post this post)
Model:
class Price(models.Model):
HourlyPrice = MoneyField(default_currency="GBP",max_digits=19,decimal_places=3,null=True,blank=False) # price per hour
DailyPrice = MoneyField(default_currency="GBP",max_digits=19,decimal_places=2,null=True,blank=True)# price per day
WeeklyPrice = MoneyField(default_currency="GBP",max_digits=19,decimal_places=2,null=True,blank=True)# price per week
MonthlyPrice = MoneyField(default_currency="GBP",max_digits=19,decimal_places=2,null=True,blank=True)# price per month
View:
def test(request):
if request.method == 'POST':
form = Price(request.POST)
form.save()
return render(request,'app/index.html',
{
'title':'Home Page',
'year':datetime.now().year,
}
)
else:
form = TestPrice(request.GET)
context = {
'form': form,
}
return render(request,'app/test.html',context)
Template:
{% extends "app/layout.html" %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
<table>
{{form}}
</table>
<button type="submit">Submit</button>
</form>
{% endblock %}
Stack Trace:
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\fields\__init__.py", line 2018, in get_prep_value
return int(value)
The above exception (int() argument must be a string, a bytes-like object or a real number, not 'QueryDict') was the direct cause of the following exception:
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\handlers\exception.py", line 56, in inner
response = get_response(request)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\OneDrive\c_CodingFiles\SqlLite Learning\testing datetime\test\test\app\views.py", line 52, in test
form.save()
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\base.py", line 812, in save
self.save_base(
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\base.py", line 863, in save_base
updated = self._save_table(
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\base.py", line 976, in _save_table
updated = self._do_update(
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\base.py", line 1019, in _do_update
filtered = base_qs.filter(pk=pk_val)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 1421, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 1439, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 1446, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\query.py", line 1532, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\query.py", line 1562, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\query.py", line 1478, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\query.py", line 1303, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\lookups.py", line 341, in get_prep_lookup
return super().get_prep_lookup()
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\lookups.py", line 85, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\fields\__init__.py", line 2020, in get_prep_value
raise e.__class__(
Thank you for your time
Likely one of the main problems is that you use Price
as a form, but Price
is a model, so passing request.POST
(or request.GET
) to it directly makes no sense.
You define a ModelForm
:
from django import forms
class PriceForm(forms.ModelForm):
class Meta:
model = Price
fields = '__all__'
then you use that PriceForm
in the view to validate the data and save the data in the database:
from django.shortcuts import redirect
def test(request):
if request.method == 'POST':
form = PriceForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('name-of-some-view')
else:
form = PriceForm()
return render(request, 'app/test.html', {'form': form})
it makes no sense to work with request.GET
in case of a GET request: in that case we perform the first request for that page, and thus we should render an empty form, not a form bounded by (possibly) empty data that then shows errors.
Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be:
hourly_price
instead of.HourlyPrice
Note: In case of a successful POST request, you should make a
redirect
[Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.