I am trying to remove a field in a many to many relationship but I came across this error and I dont know how to solve/fix it.
Here is what I am trying to do:
1: Before the below views I get a valid POST from user identifing data.log_order
2: I search if there are previous logs in ActiveSession. If there is search for matching logs with the same received data.log_order and delete the existing one and add the new received data.
3: If there is no matching data.log_order in log or previous logs to add the new received data.
As simple as the logic as difficult as I am struggling to get my errors fixed.
class Log(models.Model):
...................................
log_order = models.IntegerField(validators=[MinValueValidator(1)],blank=True, null=True)
class LogForm(forms.Form):
.............................
log_order = forms.IntegerField()
class ActiveSession(models.Model):
log = models.ManyToManyField(Log, related_name='savedlogs')
..................................
Here is the views:
if active_session.log.values():
print(active_session.log.values())
for i in active_session.log.values():
if i['log_order']==int(data.log_order):
log_order = data.log_order
log=Log.objects.filter(log_order=log_order)
print(log)
active_session = ActiveSession.objects.get(id=ActiveSession.objects.last().id,log__in=log)
print(active_session)
active_session.log.remove(active_session.log)
active_session.log.add(data)
print(active_session.log.values())
else:
active_session.log.add(data)
print(active_session.log.values())
print('Existing Log with different order_no')
else:
active_session.log.add(data)
print('Log added to Session')
print(active_session.log.values())
return HttpResponseRedirect(url)
Here is the traceback:
Traceback (most recent call last):
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1774, i
n get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'ManyRelatedManager'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in in
ner
response = get_response(request)
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_r
esponse
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\Desktop\Gym_App\my_gym\views.py", line 115, in addlog
active_session.log.remove(active_session.log)
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\fields\related_descriptors.py", l
ine 967, in remove
self._remove_items(self.source_field_name, self.target_field_name, *objs)
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\fields\related_descriptors.py", l
ine 1197, in _remove_items
self.through._default_manager.using(db).filter(filters).delete()
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\query.py", line 942, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\query.py", line 962, in _filter_o
r_exclude
clone._filter_or_exclude_inplace(negate, *args, **kwargs)
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\query.py", line 969, in _filter_o
r_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\sql\query.py", line 1360, in add_
q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\sql\query.py", line 1379, in _add
_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\sql\query.py", line 1239, in buil
d_filter
return self._add_q(
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\sql\query.py", line 1379, in _add
_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\sql\query.py", line 1321, in buil
d_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\sql\query.py", line 1167, in buil
d_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\fields\related_lookups.py", line
59, in get_prep_lookup
self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\fields\related_lookups.py", line
59, in <listcomp>
self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
File "C:\Users\User\Desktop\Portfolio\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1776, i
n get_prep_value
raise e.__class__(
TypeError: Field 'id' expected a number but got <django.db.models.fields.related_descriptors.create_forward_many_t
o_many_manager.<locals>.ManyRelatedManager object at 0x04E93418>.