好吧,我正在尝试从特定用户的关注者列表中添加用户或从中删除用户,并且如果正确的话,要切换用户的个人资料,但是当我点击关注按钮时,它向我显示此错误。我该如何解决?
models.py
class ProfileManager(models.Manager):
def toggle_follow(self, request_user, username_to_toggle):
profile_ = UserProfile.objects.get(user__username__iexact=request_user)
user = request_user
is_following = False
if username_to_toggle in profile_.follower.all():
profile_.follower.remove(username_to_toggle)
else:
profile_.follower.add(username_to_toggle)
is_following = True
return profile_, is_following
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
follower = models.ManyToManyField(User, related_name ='is_following',blank=True,)
avatar = models.ImageField(("Avatar"), upload_to='displays', default = '1.jpg',height_field=None, width_field=None, max_length=None,blank = True)
create_date = models.DateField(auto_now_add=True,null=True)
objects = ProfileManager()
viewspy
class UserProfileFollowToggle(View):
def post(self, request, *args, **kwargs):
username_to_toggle = request.POST.get("username")
profile_, is_following = UserProfile.objects.toggle_follow(request.user, username_to_toggle)
return redirect(f'/profiles/{username_to_toggle}')
回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/profiles/user-profile/
Django Version: 3.0.3
Python Version: 3.8.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'accounts',
'posts',
'profiles']
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 (most recent call last):
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\fields\__init__.py", line 1772, in get_prep_value
return int(value)
The above exception (invalid literal for int() with base 10: 'handsome') was the direct cause of the following exception:
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\AHMED\grapPub\grabpublic\profiles\views.py", line 27, in post
profile_, is_following = UserProfile.objects.toggle_follow(request.user, username_to_toggle)
File "C:\Users\AHMED\grapPub\grabpublic\profiles\models.py", line 21, in toggle_follow
profile_.follower.add(username_to_toggle)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\fields\related_descriptors.py", line 944, in add
self._add_items(
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1123, in _add_items
self.through._default_manager.using(db).bulk_create([
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\query.py", line 492, in bulk_create
returned_columns = self._batched_insert(
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\query.py", line 1230, in _batched_insert
self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1383, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1326, in as_sql
value_rows = [
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1327, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1327, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1268, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\fields\related.py", line 939, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\fields\__init__.py", line 821, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\fields\__init__.py", line 2365, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\AHMED\anaconda3\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
raise e.__class__(
Exception Type: ValueError at /profiles/user-profile/
Exception Value: Field 'id' expected a number but got 'handsome'.
如果需要更多信息而不是告诉我,我将使用该信息更新我的问题。
答案 0 :(得分:1)
def toggle_follow(self, request_user, username_to_toggle):
profile_ = UserProfile.objects.get(pk=request_user.id)
is_following = False
follower = profile_.follower.filter(username__iexact=username_to_toggle).first()
if follower:
profile_.follower.remove(follower.id)
else:
new_follower = User.objects.get(username__iexact=username_to_toggle)
profile_.follower.add(new_follower.id)
is_following = True
return profile_, is_following