Django exists query not work in if and else clause? - mysql

In given below if Medicine table consists medicine_name then query execute fine, but when medicine name doesn't exist on the Corresponding table then error is occured.
Matching Query Doesn't Exists
views.py Code
#api_view(['POST'])
def addPeople(request):
m = People()
m.bp_no = request.POST['bp_no']
m.name = request.POST['name']
m.corporation_name = request.POST['corporation_name']
m.medicine_name = request.POST['medicine_name']
m.no_of_medicine = request.POST['no_of_medicine']
existing = Medicine.objects.get(medicine_name=m.medicine_name).no_of_medicine - int(m.no_of_medicine)
p_key = Medicine.objects.get(medicine_name=m.medicine_name).id
if Medicine.objects.filter(medicine_name=m.medicine_name).exists():
if existing > 0:
m.save()
Medicine.objects.filter(id=p_key).update(no_of_medicine=existing)
return Response({"message": "Successfully Recorded"})
else:
return Response({"message": "Not much Medicine Stored"})
else:
return Response({"message": "Medicine is not Stored"})
models.py
class People(models.Model):
bp_no = models.IntegerField(blank=False,null=False)
name = models.CharField(blank=False,null=False,max_length=200)
corporation_name = models.CharField(blank=False,null=False,max_length=200)
medicine_name = models.CharField(blank=False,null=False,max_length=200)
no_of_medicine = models.IntegerField()
class Medicine(models.Model):
medicine_name = models.CharField(null=False,blank=False,max_length=200)
no_of_medicine = models.IntegerField(null=False,blank=False)
def __str__(self):
return self.medicine_name
Error Traceback: When Medicine table doesn't contains the corresponding filter name then This error will be shown
Internal Server Error: /api/add-people/
Traceback (most recent call last):
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\decorators.py", line 50, in handler
return func(*args, **kwargs)
File "E:\django\backend\medirecords\api\views.py", line 37, in addPeople
existing = Medicine.objects.get(medicine_name=m.medicine_name).no_of_medicine - int(m.no_of_medicine)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\db\models\query.py", line 435, in get
raise self.model.DoesNotExist(
api.models.Medicine.DoesNotExist: Medicine matching query does not exist.

I haven't tested this, but purely in terms of handling objects not found, I have a suggestion you can try to simplify the conditions / flow.
.get(): Wrap in try / except
get(*args, **kwargs) raises Model.DoesNotExist when not found.
When there's a not found, you need to determine how you want to handle it. For example, you can have a default fallback value, or raise an error.
In this case the code would fail if the existing medicine wasn't found, so I will have it raise.
#api_view(["POST"])
def addPeople(request):
medicine_name = request.POST["medicine_name"]
try:
medicine = Medicine.objects.get(medicine_name=medicine_name)
except Medicine.DoesNotExist:
return Response({"message": "Not much Medicine Stored"})
m = People()
m.bp_no = request.POST["bp_no"]
m.name = request.POST["name"]
m.corporation_name = request.POST["corporation_name"]
m.medicine_name = medicine_name
m.no_of_medicine = request.POST["no_of_medicine"]
existing = medicine.no_of_medicine - int(m.no_of_medicine)
p_key = medicine.id
if medicine.exists():
if existing > 0:
m.save()
medicine.num_of_medicine = existing
medicine.save(update_fields=["num_of_medicine"])
return Response({"message": "Successfully Recorded"})
else:
return Response({"message": "Not much Medicine Stored"})
else:
return Response({"message": "Medicine is not Stored"})
Move object check earlier
Since the request relies on Medicine, this is should be caught early as possible.
Uses try/catch as .get() will always raise when object is not found
The alternative is to do .exists() or filter(*args, **kwargs) + .first()
Update dependent model via Model.save() and limit field updated to num_of_medicine via update_fields.
get_object_or_404()
get_object_or_404(klass, *args, **kwargs) can also be used in lieu of the earlier try/catch block:
from django.shortcuts import get_object_or_404
medicine = get_object_or_404(Medicine.objects.all(), medicine_name=m.medicine_name)
existing = medicine.no_of_medicine - int(m.no_of_medicine)
Final note
I'm assuming this is an example, but it also may be good to validate request.POST["no_of_medicine"] in the POST data against a backend source of truth to make sure it's not faked.

Related

SQLalchemy | fastAPI. Trying to add a new row in association table gives KeyError

So I have a fastAPI project that uses sqlalchemy to connect to a postgresql database.
I use alembic to build the database and have version control on it.
My issue is now that everything works quite like it should with the many to many relationship. All stocks show the portfolio they belong to, and all portfolios show the stocks they have in them (This was tested with manually entering the data in PGAdmin).
The issue now is that every time I try to add a new connection (add a stock to a portfolio) it throws me a "Key-Error".
Here is the models.py:
class PortfolioStock(Base):
__tablename__ = "portfolio_stocks"
id = Column(Integer, primary_key=True)
stock_id = Column(Integer, ForeignKey("stocks.id", ondelete="CASCADE"))
portfolio_id = Column(Integer, ForeignKey("portfolios.id", ondelete="CASCADE"))
count = Column(Integer, nullable=True)
buy_in = Column(Float, nullable=True)
stock = relationship("Stock", back_populates="portfolios")
portfolio = relationship("Portfolio", back_populates="stocks")
#a lot of these proxy associations here because of another bug I encountered
#basically every column in the two models associated here have a association proxy row.(couldn't find another fix)
stock_created_at = association_proxy(target_collection="stock", attr="created_at")
portfolio_name = association_proxy(target_collection="portfolio", attr="name")
def __init__(self, portfolio=None, stock=None, buy_in=None, count=None):
self.portfolio = portfolio
self.stock = stock
self.buy_in = buy_in
self.count = count
class Stock(Base):
__tablename__ = "stocks"
id = Column(Integer, primary_key=True, nullable=False)
...
status = Column(Integer, nullable=False, server_default="0")
portfolios = relationship("PortfolioStock", back_populates="stock")
class Portfolio(Base):
__tablename__ = "portfolios"
id = Column(Integer, primary_key=True, nullable=False)
name = Column(String, nullable=False)
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
monetary_goal = Column(Float, nullable=True)
dividends_goal = Column(Float, nullable=True)
created_at = Column(TIMESTAMP(timezone=True), nullable=False, server_default=text("now()"))
stocks = relationship("PortfolioStock", back_populates="portfolio")
Here are some snippets to show what I do in the fastapi route.
First i get the portfolio in question and the stocks that are passed into the body.
I iterate over the stocks and grab the corresponding entry in the DB for each one.
And finally I try to just add these stocks to the list that contains all the models that are in the portfolio.
portfolio = db.query(models.Portfolio).filter(models.Portfolio.id == portfolio_id).first()
for stock in stock_ids:
stock = db.query(models.Stock).filter(models.Stock.id == stock_id).first()
portfolio.stocks.append(stock)
setattr(stock, "status", "1")
db.add(portfolio)
db.commit()
db.refresh(portfolio)
return portfolio
And here is the error message that I receive when I try to call this route from postman.
Traceback (most recent call last):
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/fastapi/applications.py", line 259, in __call__
await super().__call__(scope, receive, send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/middleware/cors.py", line 84, in __call__
await self.app(scope, receive, send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/routing.py", line 259, in handle
await self.app(scope, receive, send)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/routing.py", line 61, in app
response = await func(request)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/fastapi/routing.py", line 222, in app
raw_response = await run_endpoint_function(dependant=dependant, values=values, is_coroutine=is_coroutine)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/fastapi/routing.py", line 159, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/starlette/concurrency.py", line 39, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/anyio/to_thread.py", line 28, in run_sync
return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 818, in run_sync_in_worker_thread
return await future
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 754, in run
result = context.run(func, *args)
File "/home/maxi/dev/./api/routers/stock.py", line 151, in add_stock_to_portfolio
portfolio.stocks.append(stock)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/sqlalchemy/orm/collections.py", line 1169, in append
item = __set(self, item, _sa_initiator)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/sqlalchemy/orm/collections.py", line 1134, in __set
item = executor.fire_append_event(item, _sa_initiator)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/sqlalchemy/orm/collections.py", line 753, in fire_append_event
return self.attr.fire_append_event(
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/sqlalchemy/orm/attributes.py", line 1330, in fire_append_event
value = fn(state, value, initiator or self._append_token)
File "/home/maxi/dev/captstone_venv/lib/python3.8/site-packages/sqlalchemy/orm/attributes.py", line 1632, in emit_backref_from_collection_append_event
child_impl = child_state.manager[key].impl
KeyError: 'portfolio'
I have already tried to rename all relationships to figure out where exactly the error is coming from.
And it seems to be that t he relationship of stock = relationship("Stock", back_populates="portfolios") is a culprit of the issue.
Looking in debugging mode it seems that the model I'm trying to access is not having the correct keys in it, so my guess is that I'm either making the new association wrong and it's not portfolio.stocks.append(stock) (which worked before I fixed another issue) or that the relationships are wrong / something is missing to make it work.
I would really greatly appreciate some suggestions.
If you need additional information please let me know.

How to return single object as a json response inside django function?

I just want to get a single object of User model as a json response. I couldn't figure out how can i do that..
right now i'm getting an error that 'User' object is not iterable
Here is my function:
def some_view(username_to_toggle):
print(username_to_toggle,"User to togle")
user = User.objects.get(username__iexact=username_to_toggle)
print(user,"User object")
user_json = serializers.serialize('json', user,many=False)
print(user,"User json")
return HttpResponse(user_json, content_type='application/json')
TraceBack :
Traceback (most recent call last):
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/contrib/auth/mixins.py", line 52, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/profiles/views.py", line 65, in post
profile_, is_following,json_follower = UserProfile.objects.toggle_follow(request.user, request.user.id ,username_to_toggle,json_follower)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/profiles/models.py", line 72, in toggle_follow
json_follower = some_view(username_to_toggle)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/profiles/models.py", line 45, in some_view
user_json = serializers.serialize('json', user,many=False)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/serializers/__init__.py", line 128, in serialize
s.serialize(queryset, **options)
File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/serializers/base.py", line 90, in serialize
for count, obj in enumerate(queryset, start=1):
TypeError: 'User' object is not iterable
The serialize method from django.core.serializers doesn't have many option and it always needs a list or a queryset of model objects for the serialization. If you always want to serialize only one object, try using either of those:
first method, using queryset:
def some_view(username_to_toggle):
print(username_to_toggle,"User to togle")
users = User.objects.filter(username__iexact=username_to_toggle)
print(users,"User objects")
user_json = serializers.serialize('json', users)
print(user,"User json")
return HttpResponse(user_json, content_type='application/json')
or a second method, using list:
def some_view(username_to_toggle):
print(username_to_toggle,"User to togle")
user = User.objects.get(username__iexact=username_to_toggle)
print(user,"User object")
user_json = serializers.serialize('json', [user])
print(user,"User json")
return HttpResponse(user_json, content_type='application/json')
many=False or many=True is a feature of Django REST Framework serializators, but their usage is different and more complex as you need to declare a separate serializer for every Model you want to serialize.

Confusion in form.save() in two different models

I am having problems in displaying the page due to models. I honestly don't know what the problem is and every solution I see here is beyond me, since I am only beginning to learn Django. Here's the summary:
I have created two forms from two models(which are interrelated) and rendering the data from those forms in a single page. The data displays fine on the dashboard panel and the submission does save it to the database. I have tried overriding the save function in form but with no result. However, whenever I try and load the pages containing same data (display pages like home for customers)I am hit with:
Traceback (most recent call last):
File "C:\Users\shahi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\shahi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\shahi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Softwarica\3rd semester\Modern Web\Assignment\Project - Farmer's Market\farmersmarket\home\views.py", line 9, in index
order, created = Order.objects.get_or_create(customer=customer, complete=False)
File "C:\Users\shahi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\shahi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 559, in get_or_create
return self.get(**kwargs), False
File "C:\Users\shahi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 419, in get
raise self.model.MultipleObjectsReturned(
shop.models.Order.MultipleObjectsReturned: get() returned more than one Order -- it returned 2!
The code in forms.py:
from django.forms import ModelForm
from shop.models import *
class OrderForm(ModelForm):
class Meta:
model = Order
fields = ('customer',)
class OrderItemForm(ModelForm):
class Meta:
model = OrderItem
fields = ('item', 'status',)
views.py
def createOrder(request):
form_order = OrderForm()
form_orderitem = OrderItemForm()
if request.method == 'POST':
# print('Printing POST', request.POST)
form_order = OrderForm(request.POST)
form_orderitem = OrderItemForm(request.POST)
if form_order.is_valid() and form_orderitem.is_valid():
form_order.save()
form_orderitem.save()
return redirect('/dashboard')
context = {'form_order': form_order, 'form_orderitem': form_orderitem}
return render(request, 'dashboard/order_form.html', context)
I have tried overriding the save function in form but with no result.
get() cannot be used for when more than 1 objects are returned. try with filter().
Do check first() or last(), if it helps you.

The database backend does not accept 0 as a value for AutoField

I have a model:
class Tour(models.Model):
INACTIVE = 0
ACTIVE = 1
ARCHIVE = 2
STATUS = (
(INACTIVE, "Inactive"),
(ACTIVE, "Active"),
(ARCHIVE, "Archive"),
)
AIR_TOUR = 0
GROUND_TOUR = 1
SEA_TOUR = 2
T_TYPES = (
(AIR_TOUR, "Air_tour"),
(GROUND_TOUR, "Ground_tour"),
(SEA_TOUR, "Sea_tour"),
)
title = models.CharField(max_length=200)
tour_from = models.ForeignKey(Airport, related_name='from_location')
tour_to = models.ForeignKey(Airport, related_name='to_location')
duration_day = models.IntegerField(default=1, blank=True, null=True)
duration_night = models.IntegerField(default=1, blank=True, null=True)
transport_type = models.IntegerField(default=AIR_TOUR, choices=T_TYPES, blank=True, null=True)
documents = models.TextField(default='', blank=True, null=True)
description = models.TextField(blank=True, null=True, default='')
services = models.TextField(blank=True, null=True, default='')
airline = models.ForeignKey(Airline, null=True, blank=True)
train = models.ForeignKey(Train, null=True, blank=True)
bus = models.ForeignKey(Bus, null=True, blank=True)
user = models.ForeignKey(User, related_name='user')
creator = models.ForeignKey(User, related_name='creator')
status = models.IntegerField(default=INACTIVE, choices=STATUS, blank=True, null=True)
create_at = models.DateTimeField(default='2000-10-10')
update_at = models.DateTimeField(default='2000-10-10')
I try to change status to ARCHIVE by this code:
test = Tour.objects.get(id=self.kwargs['pk'])
test.status = ARCHIVE
test.save()
and get this error:
The database backend does not accept 0 as a value for AutoField.
status field is not AutoField. Why this error happend?
UPDATE:
full stack trace:
Django version 1.11, using settings 'project.settings_local'
Starting development server at http://0.0.0.0:8888/
Quit the server with CONTROL-C.
Internal Server Error: /dashboard/tour/delete/1/
Traceback (most recent call last):
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/py/project/dashboard/views_tour.py", line 196, in post
tour.save()
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 903, in _save_table
forced_update)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 953, in _do_update
return filtered._update(values) > 0
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/query.py", line 661, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1191, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 863, in execute_sql
sql, params = self.as_sql()
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1157, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/related.py", line 963, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save
prepared=False)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 955, in get_db_prep_value
value = connection.ops.validate_autopk_value(value)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 155, in validate_autopk_value
raise ValueError('The database backend does not accept 0 as a '
ValueError: The database backend does not accept 0 as a value for AutoField.
Internal Server Error: /dashboard/tour/delete/1/
Traceback (most recent call last):
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/py/project/dashboard/views_tour.py", line 196, in post
tour.save()
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 903, in _save_table
forced_update)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 953, in _do_update
return filtered._update(values) > 0
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/query.py", line 661, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1191, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 863, in execute_sql
sql, params = self.as_sql()
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1157, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/related.py", line 963, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save
prepared=False)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 955, in get_db_prep_value
value = connection.ops.validate_autopk_value(value)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 155, in validate_autopk_value
raise ValueError('The database backend does not accept 0 as a '
ValueError: The database backend does not accept 0 as a value for AutoField.
You likely have a foreign key field that contains a 0 (instead of null, or a valid value). If you don't have the proper foreign key constraints at the DB level (or you turned off foreign key checks when you added the constraints), there is nothing preventing invalid foreign key references from existing in that row.
Check the data in the all your foreign key fields for a 0: tour_from, tour_to,
airline, train, bus, user, creator.
What you're doing should work. Since the object is already in the database, it's strange that updating it with the same fields leads to a 0 somewhere (probably one of the foreign keys).
You should debug this and there are two ways to go at it:
Inspect your database using raw SQL. manage.py dbshell opens a SQL shell where you can do SELECT * FROM my_app_tour WHERE id = 1. Look at the particular row, maybe you can spot the issue.
Try test.save(update_fields=['status']). If that works, add more fields to update one by one, e.g. test.save(update_fields=['status', 'tour_from'], then test.save(update_fields['status', 'tour_from', 'tour_to'])... until the error is thrown again.
I suspect a database corruption, but there might be some other more serious issue in the way you create the objects. That would cause problems down the line.
Check your django_content_type table, look for 0's in the id column. Django logs database operations after each call, and it writes the ID of the content type.
I found that I had several entries with a 0 id. Updating these with unique numbers after the end of the sequence and restarting Django fixed this for me.
Honestly, I have no idea how it got that way. I'm using Django 2.2.

Handling Django form.save() ValueError

I am a newbie to Django error handling and I spent a week trying to figure out what exactly goes wrong when trying to update a MySQL table using Django forms. I end up with ValueError: invalid literal for int() with base 10: '\x01' error. I tried to surround the erroneous code with try catch block trapping valueError and printing the row sql query, and here's what I get.
The code:
def updateTask(request, task_id):
#cur_usr_sale_point = PersonUnique.objects.filter(employees__employeeuser__auth_user = request.user.id).values_list('agreementemployees__agreement_unique__sale_point_id',flat=True)
selected_task = Tasks.objects.get(id=task_id)
responsible_people = TaskResponsiblePeople.objects.get(task_id = task_id)
task_table = Tasks. objects.all()
if request.method == 'POST':
task_form = TaskForm(request.POST,instance=selected_task)
responsible_people_form = TaskResponsiblePeopleForm(request.POST, instance = responsible_people)
if task_form.is_valid() and responsible_people_form.is_valid():
responsible_people_instance = responsible_people_form.save(commit=False)
try:
responsible_people_instance.task = task_form.save()
responsible_people_form.save()
except ValueError:
from django.db import connection
print connection.queries[-1]
return HttpResponseRedirect(reverse('task_list'))
Print gives me an absolutely valid MySQL Select-statement (to my surpise, I expected an update-statement).
The traceback without try-catch block:
Internal Server Error: /task_list/update_task/200/
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\\10.8.0.1\share\djprj\djprj\djprj\task\views.py", line 101, in updateTask
task_form.save();
File "C:\Python27\lib\site-packages\django\forms\models.py", line 451, in save
self.instance.save()
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 700, in save
force_update=force_update, update_fields=update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 728, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 793, in _save_table
forced_update)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 843, in _do_update
return filtered._update(values) > 0
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 645, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 1149, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 837, in execute_sql
sql, params = self.as_sql()
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 1117, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 728, in get_db_prep_save
prepared=False)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 720, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1853, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: '\x01'
[10/Apr/2016 11:15:46] "POST /task_list/update_task/200/ HTTP/1.1" 500 126245
Help me out please !!!
EDIT: added is_valid method
You need to first use the form's is_valid method:
A Form instance has an is_valid() method, which runs validation routines for all its fields. When this method is called, if all fields contain valid data.
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
Source: Django Docs: Working with forms
I would recommend that you read the above documentation.