首页 > 解决方案 > 当该字段仅存在于子类中时,为什么 Django 告诉我该字段与基类中的字段发生冲突?

问题描述

我正在尝试定义一些从带有时间戳的基类继承的模型,但出现以下错误:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/matt/Repositories/mtm/env/lib/python3.5/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/matt/Repositories/mtm/env/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/matt/Repositories/mtm/env/lib/python3.5/site-packages/django/core/management/base.py", line 436, in check
    raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
events.Event.location: (models.E006) The field 'location' clashes with the field 'location' from model 'home.timestampedmodel'.

System check identified 1 issue (0 silenced).

这是我的模型:

主页/models.py

from django.db import models

class TimestampedModel(models.Model):
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

事件/模型.py

from django.db import models
from home.models import TimestampedModel

class Location(TimestampedModel):
    name = models.CharField(max_length=200)
    address1 = models.CharField(max_length=200)
    address2 = models.CharField(max_length=200)
    city = models.CharField(max_length=80, default='Chicago')
    state = models.CharField(max_length=2, default='IL')

class Event(TimestampedModel):
    title = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    date_start = models.DateTimeField()
    date_end = models.DateTimeField(null=True, blank=True)
    location = models.ForeignKey(Location, on_delete=models.CASCADE)

当我拥有LocationEvent继承自时,代码成功运行models.Model,但这不是我希望代码的结构方式。

events/models.py(有效,但不可取)

from django.db import models

class Location(models.Model):
    name = models.CharField(max_length=200)
    address1 = models.CharField(max_length=200)
    address2 = models.CharField(max_length=200)
    city = models.CharField(max_length=80, default='Chicago')
    state = models.CharField(max_length=2, default='IL')
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

class Event(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    date_start = models.DateTimeField()
    date_end = models.DateTimeField(null=True, blank=True)
    location = models.ForeignKey(Location, on_delete=models.CASCADE)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

为什么我会遇到这个错误?有什么方法可以在我的模型中包含date_createddate_updated而不是每次都明确定义它们?

标签: pythondjango

解决方案


尝试使 timestampedmodel 抽象

class TimestampedModel(models.Model):
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

推荐阅读