时间戳模型 __init__() 获得参数 'on_delete' 的多个值

Timestamped model __init__() got multiple values for argument 'on_delete'

我正在尝试使用 on_delete

在 DRF 模型中创建外键关系
fk_city = models.ForeignKey("region_mgmt.City", "warehouses", on_delete=models.SET_NULL)    
TypeError: __init__() got multiple values for argument 'on_delete'

下面是我的代码:

from django_extensions.db.models import TimeStampedModel

class State(TimeStampedModel, models.Model):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    name = models.CharField(max_length=100)
    postal_code = models.CharField(max_length=20, blank=True, null=True)
    fk_country = models.ForeignKey(Country, related_name="states", on_delete=models.CASCADE)

    def __str__(self):
        return self.name
class City(TimeStampedModel, models.Model):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    name = models.CharField(max_length=100)
    postal_code = models.CharField(max_length=20, blank=True, null=True)
    fk_state = models.ForeignKey(State, related_name="cities", on_delete=models.CASCADE)

    def __str__(self):
        return self.name

在另一个模块中我有以下模型

class Warehouse(TimeStampedModel, models.Model):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    name = models.CharField(max_length=255, null=False, blank=False)
    address = models.TextField(null=False, blank=False)
    fk_city = models.ForeignKey("region_mgmt.City", "warehouses", on_delete=models.SET_NULL)
    contact_no = models.CharField(max_length=100)

有谁知道原因和解决方法吗?

ForeignKey 的第二个参数是 on_delete=… parameter [Django-doc], so you specify two values for this. Likely you want to use your warehouses as related_name=… parameter [Django-doc],因此您将其实现为:

class Warehouse(TimeStampedModel, models.Model):
    # …
    fk_city = models.ForeignKey(
        'region_mgmt.City',
        <strong>related_name='warehouses'</strong>,
        on_delete=models.SET_NULL,
        null=True
    )