无法分配“...”:“...”必须是 "User" 个实例

Cannot assign "...'": "..." must be a "User" instance

models.py 求助

class AssistanceQuest (models.Model):     

    Customer = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=True,null=False)
    Status = models.BooleanField(default=False,null=False,blank=False)    
    Date_request = models.DateTimeField(auto_now_add=False)
    Tipology = models.CharField(max_length=50, null= False)
    Request = models.TextField(max_length= 1000, blank=False, null=False)

models.py 自定义用户

from django.contrib.auth.models import AbstractUser
from .managers import CustomUserManager

class User (AbstractUser):
   is_superuser = None
   is_staff = None
   groups = None
   user_permissions = None
   objects = CustomUserManager()

manager.py 自定义用户

 from django.contrib.auth.base_user import BaseUserManager
 from django.utils.translation import ugettext_lazy as _


class CustomUserManager(BaseUserManager):
"""
Custom user model manager senza staff o permessi
"""
def create_user(self, username,email,first_name,last_name,password):
  
    if not email:
        raise ValueError(_('The Email must be set'))
    email = self.normalize_email(email)        
    user = self.model(username = username, email = email, first_name = first_name, last_name = last_name)
    user.set_password(password)
    user.save()
    return user

这是view.py的帮助

    if User.is_authenticated:
                
        Message = "Assistance Request From " + str(User.get_full_name(User)) + ": "+ str(User.email)
        
      
    else:
        Message = "Assistance Request From " + request.POST['message-name-surname']+"\n Email: "+ request.POST['message-email']
    
    Message += "\n City: "+ request.POST['message-city']+"\n Province: "+ request.POST['message-province']+"\n Landline Or Mobile Phone: "+ request.POST['message-phone']+"\n Number Order Or Purchase Period: "+ request.POST['message-number-order']+"\n Product Under Warranty: "+ request.POST['message-warranty']+"\n Request: "+ request.POST['message']+"\n Prefers To Be Contacted: "+ request.POST['message-pref-contact']
    
    if User.is_authenticated:
        user= settings.AUTH_USER_MODEL
        Assistance = AssistanceQuest(Customer=user, Status= 'False',Request= Message, Tipology='Tecnica',Date_request=datetime.datetime.now())
        Assistance.save()
        Subject = "Assistance Request From " + str(User.get_username(User))
        send_mail(
            Subject,
            Message,
            'artemis.pcstore@gmail.com', 
        ['artemis.pcstore@gmail.com'],
        )
    else:
        send_mail(
            "Richiesta non registrata di assistenza",
            Message,
            'artemis.pcstore@gmail.com', 
        ['artemis.pcstore@gmail.com'],
        )

当我尝试插入请求时,我得到的唯一错误是: /Assistance/ 的 ValueError 无法分配“'User.User'”:“AssistanceQuest.Customer”必须是“用户”实例。

有人可以帮助我吗?

Customer 字段应该是一个 User 实例,但是您在创建 AssistanceQuest 时将模型实例传递给 Customer 字段。

所以改变这个

Assistance = AssistanceQuest(Customer=user, Status= 'False',Request= Message, Tipology='Tecnica',Date_request=datetime.datetime.now())

Assistance = AssistanceQuest(<b>Customer=request.user</b>, Status= 'False',Request= Message, Tipology='Tecnica',Date_request=datetime.datetime.now())

假设经过身份验证的用户是客户