python - 当该字段仅存在于子类中时,为什么 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)
当我拥有Location
并Event
继承自时,代码成功运行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_created
和date_updated
而不是每次都明确定义它们?
解决方案
尝试使 timestampedmodel 抽象
class TimestampedModel(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
推荐阅读
- cordova - 从 idChamp® RS4 BLE RFID 设备获取卡信息
- php - 从 php webservice 打印值(json 对象)
- python - 警告:(1265,“第 1 行的“日期”列的数据被截断”)
- angular - 如何在没有 ngFor 的情况下使用 Angular 5 更新数据
- npm - 使用终端在本地运行 npm 脚本
- java - 无法为既未配置也未配置任何子类型的类型“xyzpackage”构建查询
- python - 导入谷歌库的Python 2to3转换
- python - 在按下按钮之前如何不掷硬币?Python图形用户界面
- image - tkinter 文本小部件中的图像
- c - 为什么索引 1 处的数组在我的编译器中重置?(未定义的行为)