Django Admin - 'bool' object is not callable - django

When I try and delete records within Django Admin, for some records, I am getting
'bool' object is not callable
I cannot work out where the error is based on this Traceback.
Traceback:
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
575. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
62. return bound_func(*args, **kwargs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
58. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/contrib/admin/options.py" in delete_view
1736. return self._delete_view(request, object_id, extra_context)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/contrib/admin/options.py" in _delete_view
1760. [obj], opts, request.user, self.admin_site, using)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/contrib/admin/utils.py" in get_deleted_objects
131. collector.collect(objs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/contrib/admin/utils.py" in collect
195. return super().collect(objs, source_attr=source_attr, **kwargs)
File "/home/henry/Documents/Sites/Development/django-authenticjobs/env/lib/python3.6/site-packages/django/db/models/deletion.py" in collect
222. field.remote_field.on_delete(self, field, sub_objs, self.using)
Exception Type: TypeError at /admin/jobboard/job/155/delete/
Exception Value: 'bool' object is not callable
Can anyone suggest where to look? Thank you

You have set an on_delete of a ForeignKey, or OneToOneField to a boolean (True or False). So something like:
class SomeModel(models.Model):
some_fk = models.ForeignKey(OtherModel, on_delete=False)
You can not set this to a boolean. You can set this to the values listed in the documentation: CASCADE, PROTECT, SET_NULL, SET_DEFAULT, SET(..), or DO_NOTHING.
You could, strictly speaking, implement your own strategy as well, since the ones listed above are in fact just functions. For example the CASCADE is implemented like [GitHub]:
def CASCADE(collector, field, sub_objs, using):
collector.collect(sub_objs, source=field.remote_field.model,
source_attr=field.name, nullable=field.null)
if field.null and not connections[using].features.can_defer_constraint_checks:
collector.add_field_update(field, None, sub_objs)
although likely you do not need to implement your own, and pick one of the ones listed in the documentation. For example:
class SomeModel(models.Model):
some_fk = models.ForeignKey(OtherModel, on_delete=models.CASCADE)

Related

"Cannot call delete() after .distinct()" error after django update

I am using djangocms_blog.
Today I updated django from 3.1 to 3.2.12 and suddenly when I try to delete post object, there is "Cannot call delete() after .distinct()" error
Traceback:
env\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
response = get_response(request) …
env\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
env\lib\site-packages\django\contrib\admin\options.py, line 616, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs) …
env\lib\site-packages\django\utils\decorators.py, line 130, in _wrapped_view
response = view_func(request, *args, **kwargs) …
env\lib\site-packages\django\views\decorators\cache.py, line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs) …
env\lib\site-packages\django\contrib\admin\sites.py, line 232, in inner
return view(request, *args, **kwargs) …
env\lib\site-packages\django\utils\decorators.py, line 43, in _wrapper
return bound_method(*args, **kwargs) …
env\lib\site-packages\django\utils\decorators.py, line 130, in _wrapped_view
response = view_func(request, *args, **kwargs) …
env\lib\site-packages\django\contrib\admin\options.py, line 1739, in changelist_view
response = self.response_action(request, queryset=cl.get_queryset(request)) …
env\lib\site-packages\django\contrib\admin\options.py, line 1408, in response_action
response = func(self, request, queryset) …
env\lib\site-packages\django\contrib\admin\actions.py, line 45, in delete_selected
modeladmin.delete_queryset(request, queryset) …
env\lib\site-packages\django\contrib\admin\options.py, line 1109, in delete_queryset
queryset.delete() …
env\lib\site-packages\django\db\models\query.py, line 728, in delete
raise TypeError('Cannot call delete() after .distinct().') …
I restored Django 3.1 version and everything works fine, but working on old version isn't best solution
Fixed via setting
Django = ">=3.2,<3.3"
in pyproject.toml, so it selected itself Django 3.2.13.

How to save django manytomany field without through table

I face a vicious cycle that I cannot break out of;
Lets say I have a following model:
class AgregatorProductCoupon(models.Model):
AgregatorProductId = models.ManyToManyField(Product, related_name="coupons", db_column='AgregatorProductId', verbose_name=_("Agregator Product Id"))
class Meta:
managed = True
db_table = 'AgregatorProductCoupon'
when I try to save an instance, I get an integrity error stating
"<AgregatorProductCoupon: AgregatorProductCoupon object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.
but when I try to save without the m2m rel, I obviously get an error for a required field missing.
Cannot insert the value NULL into column 'AgregatorProductId', table 'test_dbo.dbo.AgregatorProductCoupon'; column does not allow nulls. INSERT fails.
I cannot redesign the tables as they are bound to other places too.
How to go about it?
Any help would be much appreciated.
EDIT: Traceback:
[2021-01-07 13:19:28,040] log_response - Internal Server Error: /admin/agregator/agregatorcoupons/78/change/
Traceback (most recent call last):
File "C:\David\.venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\David\.venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\David\.venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1650, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "C:\David\.venv\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1536, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1576, in _changeform_view
self.save_related(request, form, formsets, not add)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1122, in save_related
self.save_formset(request, form, formset, change=change)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1110, in save_formset
formset.save()
File "C:\David\.venv\lib\site-packages\django\forms\models.py", line 669, in save
return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "C:\David\.venv\lib\site-packages\django\forms\models.py", line 803, in save_new_objects
self.new_objects.append(self.save_new(form, commit=commit))
File "C:\David\.venv\lib\site-packages\django\forms\models.py", line 949, in save_new
obj.save()
File "C:\David\agregator\models.py", line 2745, in save
print(self.AgregatorProductId)
File "C:\David\.venv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 526, in __get__
return self.related_manager_cls(instance)
File "C:\David\.venv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 821, in __init__
raise ValueError('"%r" needs to have a value for field "%s" before '
ValueError: "<AgregatorProductCoupon: AgregatorProductCoupon object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.

Django Admin Console giving 'str' object has no attribute '__iter__' error

I am trying to make some changes in the fields using Djangos build in Admin console.It gives me 'str' object has no attribute 'iter' error exception when i try to change some fields in the models.These fields are Foreign Keys
PROJECT MODEL CAUSES ERROR WHEN I CHANGE THE MANAGER FIELD
class Project(models.Model):
"""Project Model.
"""
name = models.CharField(unique=True, max_length=255)
manager = models.ForeignKey('Employee', blank=True, null=True)
project = models.Manager()
class Employee(models.Model):
"""
Employee Model.
"""
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.CharField(
unique=True,
max_length=255,
blank=True,
null=True)
projects = models.ManyToManyField('Project', blank=True)
employee = models.Manager()
TRACEBACK
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
544. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
211. return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in change_view
1512. return self.changeform_view(request, object_id, form_url, extra_context)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in inner
185. return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in changeform_view
1451. change_message = self.construct_change_message(request, form, formsets, add)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in inner
185. return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in construct_change_message
942. elif form.changed_data:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in __get__
35. res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in changed_data
447. if field.has_changed(initial_value, data_value):
File "/usr/local/lib/python2.7/dist-packages/django/forms/fields.py" in has_changed
1075. initial = field.to_python(initial)
File "/usr/local/lib/python2.7/dist-packages/django/forms/fields.py" in to_python
424. return super(DateField, self).to_python(value)
File "/usr/local/lib/python2.7/dist-packages/django/forms/fields.py" in to_python
395. for format in self.input_formats:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in __wrapper__
112. return getattr(res, method_name)(*args, **kw)
Exception Type: AttributeError at /admin/login/project/2/change/
Exception Value: 'str' object has no attribute '__iter__'
admin.py file
from django.contrib import admin
from models import *
admin.site.register(Employee)
admin.site.register(Project)
I've had this problem myself. I upgraded from Django 1.10 to 1.10.2 and that seems to have fixed it for me. Good luck.

'User' object has no attribute '__getitem__' error

I have the following model:
class StudentUsername(models.Model):
user = models.OneToOneField(User)
student = models.ForeignKey(Student)
When I try to add a user and student to this table using default Django Admin interface, I get the following error:
Exception Type: TypeError
Exception Value: 'User' object has no attribute '__getitem__'
Kindly help.
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
432. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
198. return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\db\transaction.py" in inner
371. return func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view
1133. self.log_addition(request, new_object)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in log_addition
600. action_flag=ADDITION
File "C:\Python27\lib\site-packages\django\contrib\admin\models.py" in log_action
19. e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)
Exception Type: TypeError at /backoffice/students/studentusername/add/
Exception Value: 'User' object has no attribute '__getitem__'
It seems likely that you have defined a __unicode__ method on StudentUsername that is returning either a User or a Student object - that is, self.user or self.student - rather than actual unicode. So when Django tries to slice it, it gets this error.
Ensure that your unicode method actually returns unicode text.

Django admin 'save_as' with inlines

Should 'save_as' work with inlines. I'm rather baffled by the traceback but it doesn't seem to show much involvement of my own code.
The problem seems to be the id's of inlines being set to '' when saving an add form. This is handled quite happily by when you do a normal 'add':
Traceback:
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
307. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
197. return view(request, *args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
28. return bound_func(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
24. return func(self, *args2, **kwargs2)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/transaction.py" in inner
217. res = func(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
879. prefix=prefix, queryset=inline.queryset(request))
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/models.py" in __init__
682. queryset=qs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/models.py" in __init__
415. super(BaseModelFormSet, self).__init__(**defaults)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/formsets.py" in __init__
47. self._construct_forms()
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/formsets.py" in _construct_forms
108. self.forms.append(self._construct_form(i))
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/models.py" in _construct_form
691. form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/models.py" in _construct_form
437. connection=connections[self.get_queryset().db])
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/subclassing.py" in inner
53. return func(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/subclassing.py" in inner
53. return func(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_lookup
306. value = self.get_prep_lookup(lookup_type, value)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
292. return self.get_prep_value(value)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
479. return int(value)
Exception Type: ValueError at /admin/property_manager/property/add/
Exception Value: invalid literal for int() with base 10: ''
Could be related to this if you're using FileFields? https://code.djangoproject.com/ticket/14760