django model (models.E012) 'unique_together' refers to the non-existent field

    今天,在使用django model建表的过程中,遇到一个诡异的bug,django一直给我提示(models.E012) 'unique_together' refers to the non-existent field。我一直没明白,错误代码如下

class Test(models.Model):
    index = models.IntegerField
    score = models.IntegerField

    class Meta:
        unique_together = ('index', 'score')

    经过多次调试,发现删除meta能够建表,加上meta就报错。原因在于这时的index和score是一个字段类,在meta中,Django检测并枚举字段实例(即通过运行isinstance(v, Field))来创建字段。因此,在这种情况下,无法使用meta。我们只需加上一对括号,index和score就变成了字段实例,就能正确运行了。正确的代码如下:

class Test(models.Model):
    index = models.IntegerField()
    score = models.IntegerField()

    class Meta:
        unique_together = ('index', 'score')


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WARNINGS: ?: (staticfiles.W004) The directory 'static' in the STATICFILES_DIRS setting does not exist. buy.Cart: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the BuyConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.d b.models.BigAutoField'. buy.Orders: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the BuyConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.d b.models.BigAutoField'. sale.Brand: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SaleConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django. db.models.BigAutoField'. sale.Carinfo: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SaleConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django. db.models.BigAutoField'. userinfo.UserInfo: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the UserinfoConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'dja ngo.db.models.BigAutoField'. No changes detected
最新发布
06-02
The warnings indicate the following issues: - `staticfiles.W004`: The `STATICFILES_DIRS` setting is configured to use a directory named `static`, but it does not exist in your project. You should either create the directory or modify the setting to use an existing directory. - `models.W042`: Auto-created primary keys are being used for several of your models (`buy.Cart`, `buy.Orders`, `sale.Brand`, `sale.Carinfo`, `userinfo.UserInfo`). You should explicitly define a primary key field for each model instead of relying on the default auto-generated primary key. To do this, add a `primary_key=True` argument to a field in the model definition, or specify a custom primary key field using the `primary_key` attribute. - `No changes detected`: This is not a warning, but rather an informational message indicating that no changes were detected in your models that require migration. To address the `models.W042` warnings, you can either add a `primary_key=True` argument to an existing field or create a new field with the `primary_key=True` argument. For example: ```python class Cart(models.Model): cart_id = models.AutoField(primary_key=True) # ... ``` Alternatively, you can configure the `DEFAULT_AUTO_FIELD` setting in your project settings to specify a custom primary key field for all models in your project. For example: ```python # settings.py from django.conf import settings class BuyConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' settings.configure(BUY_CONFIG=BuyConfig) ``` This will set the default auto-generated primary key field for all models in the `buy` app to `BigAutoField`. You can repeat this pattern for other apps in your project as well.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值