You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django多对多字段报错:__call__()缺失必填关键字参数'manager'

问题描述

在Django项目的Clothes模型中添加了和Post模型likes写法类似的多对多字段save,用于实现用户收藏服饰的功能,但出现__call__() missing 1 required keyword-only argument: 'manager'错误。最初未手动设置id字段,后来将id设为IntegerField并指定primary_key=True后,仍出现相同错误。Post模型中使用相同形式的多对多字段却正常运行,怀疑问题与主键有关,想知道原因及解决办法。

相关模型代码

from django.db import models
from django.conf import settings 

# Create your models here.
  
class Clothes(models.Model):
  CATEGORYS =[
    (0, '상의'), #상의
    (1, '하의'), #하의
    (2, '아우터'), #아우터
    (3, '신발'), #신발
    (4, '악세사리'), #악세사리
  ]
  category = models.IntegerField(default=0,choices=CATEGORYS)
  id = models.IntegerField(primary_key=True)
  img = models.ImageField(upload_to='main/images/clothes/%Y/%m/%d')
  save = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='Pickitems', blank=True)
  author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  buying = models.TextField(null=True, blank=True)
  
  def __str__(self):
    return f'{self.id}: {self.category}'

class Post(models.Model):
  main_img = models.ImageField(upload_to='main/images/post/%Y/%m/%d')
  title = models.CharField(max_length=100)
  content = models.TextField()
  private = models.BooleanField(default=False)
  author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  clothes = models.ManyToManyField(Clothes,related_name='Clothes')
  likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='Likes', blank=True)
  
  def __str__(self):
    return f'{self.pk}: {self.title}'
  
  def get_absolute_url(self):
    return f'/community/'

class Comment(models.Model):
  post = models.ForeignKey(Post, on_delete=models.CASCADE)
  author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  content = models.TextField()
  create_date = models.DateTimeField(auto_now_add=True)
  update_date = models.DateTimeField(auto_now_add=True)
  
  def __str__(self):
    return f'({self.author}) {self.post.title} :  {self.content}'
  
class Commu(models.Model):
  COMMU_CHOICES = [
    ('buying', 'buying'), #공동구매
    ('openrun', 'openrun'), #오픈런
    ('question', 'question'), #고민방
  ]
  category = models.CharField(max_length=20, choices=COMMU_CHOICES)
  img = models.ImageField(upload_to='main/images/commu/%Y/%m/%d', null=True, blank=True)
  title = models.CharField(max_length=100)
  content = models.TextField()
  author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  
  def __str__(self):
    return f'{self.pk}: {self.title}'
  
  def get_absolute_url(self):
    return f'/community/commu'
原因分析

问题和主键完全无关,核心原因是你把多对多字段命名成了save——这个名称和Django模型内置的save()方法重名了!

Django每个模型都自带save()方法,用于将实例保存到数据库。你用save作为字段名,会直接覆盖模型原本的save方法,而多对多字段的管理器和内置方法逻辑冲突,就会抛出这个参数缺失的错误。Post模型用的是likes作为字段名,没有和内置方法重名,所以能正常运行。

解决办法
  • 修改字段名称:把Clothes模型里的save字段改成不冲突的名称,比如saved_users或者collectors:
    # 替换原save字段
    saved_users = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='Pickitems', blank=True)
    
  • 移除自定义主键(可选):Django会自动为模型生成AutoField类型的主键id,除非有特殊业务需求,没必要手动设置IntegerField(primary_key=True),这一步和当前错误无关,但能简化模型代码。
  • 同步更新关联代码:如果项目里其他地方(比如视图、模板)已经用到了clothes.save.add(user)这类代码,要同步改成新的字段名。
  • 执行数据库迁移:修改模型后,运行以下命令更新数据库结构:
    python manage.py makemigrations
    python manage.py migrate
    

内容的提问来源于stack exchange,提问作者won

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:02:11