Class 基于视图 - 未在数据库中创建字段(python Inherits\django cbv)
Class Based View - Fields aren't created in database (python Inherits\django cbv)
我正在尝试按照本教程创建模型继承:https://www.digitalocean.com/community/tutorials/understanding-class-inheritance-in-python-3
一切正常。可以在 child 中访问 FirstName 属性 并显示在视图中,但是当我 运行 makemigration/migrate
时,继承字段不会在 table 在数据库中。 (core-shark / core-trout)
我做错了什么?是否可以使用 python 继承和 CBV,通过 makemigration/migrate?
在数据库中创建字段
model.py
class Fish(models.Model):
def __init__(self, first_name, last_name="Fish"):
self.first_name = first_name
self.last_name = last_name
def swim(self):
print("The fish is swimming.")
def swim_backwards(self):
print("The fish can swim backwards.")
class Meta:
verbose_name = 'Fish'
abstract = True
def __str__(self):
return self.first_name
class Shark(Fish):
def __init__(self, first_name, last_name="Shark", skeleton="cartilage", eyelids=True):
self.first_name = first_name
self.last_name = last_name
self.skeleton = skeleton
self.eyelids = eyelids
def swim_backwards(self):
print("The shark cannot swim backwards, but can sink backwards.")
class Trout(Fish):
def __init__(self, water ='', price = 0):
self.water = water
self.price = price
super().__init__(self)
class Meta:
verbose_name = 'Trout'
view.py
class IndexView(TemplateView):
template_name = 'index.html'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
sammy = Shark("Sammy")
terry = Trout()
terry.first_name = "Terry"
context['sammy'] = sammy
context['terry'] = terry
return context
index.html
...
<body style="color:red">
<h1>Fish Name: {{sammy.first_name}}</h1>
<h1>Fish Name: {{terry.first_name}}</h1>
</body>
...
[2
根据 Django Documentation for Models,您需要将数据库字段定义为 Class 属性。
I.o.w:
class Fish (models.Model):
first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
def __init__(self, first_name, last_name="Fish"):
self.first_name = first_name
self.last_name = last_name
这样做将允许在您 运行 迁移时在数据库中创建它们。
我正在尝试按照本教程创建模型继承:https://www.digitalocean.com/community/tutorials/understanding-class-inheritance-in-python-3
一切正常。可以在 child 中访问 FirstName 属性 并显示在视图中,但是当我 运行 makemigration/migrate
时,继承字段不会在 table 在数据库中。 (core-shark / core-trout)
我做错了什么?是否可以使用 python 继承和 CBV,通过 makemigration/migrate?
在数据库中创建字段model.py
class Fish(models.Model):
def __init__(self, first_name, last_name="Fish"):
self.first_name = first_name
self.last_name = last_name
def swim(self):
print("The fish is swimming.")
def swim_backwards(self):
print("The fish can swim backwards.")
class Meta:
verbose_name = 'Fish'
abstract = True
def __str__(self):
return self.first_name
class Shark(Fish):
def __init__(self, first_name, last_name="Shark", skeleton="cartilage", eyelids=True):
self.first_name = first_name
self.last_name = last_name
self.skeleton = skeleton
self.eyelids = eyelids
def swim_backwards(self):
print("The shark cannot swim backwards, but can sink backwards.")
class Trout(Fish):
def __init__(self, water ='', price = 0):
self.water = water
self.price = price
super().__init__(self)
class Meta:
verbose_name = 'Trout'
view.py
class IndexView(TemplateView):
template_name = 'index.html'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
sammy = Shark("Sammy")
terry = Trout()
terry.first_name = "Terry"
context['sammy'] = sammy
context['terry'] = terry
return context
index.html
...
<body style="color:red">
<h1>Fish Name: {{sammy.first_name}}</h1>
<h1>Fish Name: {{terry.first_name}}</h1>
</body>
...
[
根据 Django Documentation for Models,您需要将数据库字段定义为 Class 属性。
I.o.w:
class Fish (models.Model):
first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
def __init__(self, first_name, last_name="Fish"):
self.first_name = first_name
self.last_name = last_name
这样做将允许在您 运行 迁移时在数据库中创建它们。