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

Django模型定义报错:Field.__init__()收到意外关键字参数'max_lenght'

Django模型定义TypeError错误解决方案:意外关键字参数'max_lenght'

错误根源

触发TypeError: Field.__init__() got an unexpected keyword argument 'max_lenght'的直接原因是参数拼写错误:将CharField的合法参数max_length误写为max_lenght,Django无法识别该关键字参数,因此抛出错误。同时代码中存在其他拼写问题,一并修正如下:

需修正的拼写错误清单

  • 模型类名:Coustomer → Customer
  • 参数名:所有max_lenght替换为max_length(涉及locality、state、title、brand、category字段)
  • 字段名:prodect_image → product_image
  • ImageField参数:uploded_to → uploaded_to

修正后的完整代码

from django.db import models
# 注意:需确保User模型已正确导入,例如 from django.contrib.auth.models import User

class Customer(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    locality = models.CharField(max_length=150)
    zipcode = models.IntegerField()
    state = models.CharField(choices=STATE_CHOICES, max_length=100)

class Product(models.Model):
    title = models.CharField(max_length=100)
    selling_price = models.FloatField()
    discount_price = models.FloatField()
    description = models.TextField()
    brand = models.CharField(max_length=100)
    category = models.CharField(choices=CATEGORY_CHOICE, max_length=2)
    product_image = models.ImageField(uploaded_to='Productimg')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 12:15:37