Skip to content

demo里的两处index_together报错 #28

@zhnny

Description

@zhnny

https://github.com/Deali-Axy/DjangoStarter/blob/5fff83bb5201e17f6d8728de887d9117cf908032/src/apps/demo/models.py#L41
demo里的两处index_together会报错:

TypeError: 'class Meta' got invalid attribute(s): index_together

DeepSeek提示:

from django.db import models

class MyModel(models.Model):
    field1 = models.CharField(max_length=100)
    field2 = models.IntegerField()

    class Meta:
        # 旧方式(Django <3.1)❌
        # index_together = [['field1', 'field2']]
        
        # 新方式(Django 3.1+)✅
        indexes = [
            models.Index(fields=['field1', 'field2']),
        ]

修改后工作正常,Windows 10平台,Django 5.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions