[英]How to set default value on a tables model that extends a seperate tables model as it's base in SQL Alchemy
I've had a good dig around various questions and seem to be missing something that I feel must be fairly obvious.我对各种问题进行了很好的挖掘,似乎遗漏了一些我认为必须相当明显的东西。
In Postgres, using Python, SQLAlchemy and FactoryBoy to run (now failing) tests, I have two table models...在 Postgres 中,使用 Python、SQLAlchemy 和 FactoryBoy 运行(现在失败)测试,我有两个表模型......
When Letter
is instantiated, it extends File
:当Letter
被实例化时,它会扩展File
:
class Letter(File):
-- --
and File
in turn extends SQL Alchemys DeclarativeBase
:和File
反过来扩展 SQL Alchemys DeclarativeBase
:
class File(DeclarativeBase):
-- --
Within File
, is a property called content_type
.在File
中,有一个名为content_type
的属性。
content_type
has now been set on File
to be not nullable
. content_type
现在已在File
上设置为not nullable
。
-- --
What I need to do feels very basic...我需要做的感觉非常基本......
Within Letter
, I need to set the default content_type
to "application/pdf"
... that's it!在Letter
中,我需要将默认content_type
设置为"application/pdf"
……就是这样!
However, for all my trying, I cannot seem to get it to set this as the default content type for Letter
但是,尽管我一直在尝试,我似乎无法将其设置为Letter
的默认内容类型
This means that tests we have running, fail on trying to create the File
as it has no content_type
to pass over to File
, meaning I get a NotNullViolation
.这意味着我们正在运行的测试在尝试创建File
时失败,因为它没有content_type
可以传递给File
,这意味着我得到了NotNullViolation
。
I have tried setting this within the Letter
model in a few different ways, but to no avail.我尝试以几种不同的方式在Letter
模型中设置它,但无济于事。
-- --
Approaches:方法:
__init__
in both the Custom Base model of File
and as well as in Letter
using kawgs
and attempting to set it that way, though this did not work.我尝试在File
的 Custom Base 模型和Letter
中使用__init__
并使用kawgs
并尝试以这种方式设置它,尽管这不起作用。Letter
model, but this defeats Third Normal Form so I assume there is a better way to do it.我曾考虑将列添加到Letter
模型中,但这违反了第三范式,所以我认为有更好的方法来做到这一点。FactoryBoy
)我试图通过我们正在运行的测试传递值,但没有任何运气(测试使用FactoryBoy
)LetterFactory
(from FactoryBoy
) which is where the issue is coming from, I have attempted to add content_type = "application/pdf"
to this without any luck.有一个LetterFactory
(来自FactoryBoy
)这是问题的来源,我试图在没有任何运气的情况下将content_type = "application/pdf"
添加到此。content_type
on the model return in the FactoryBoy - LetterFactory
, again :我还尝试在FactoryBoy - LetterFactory
中的模型返回上设置content_type
,再次:class Meta(SQLAlchemyOptions):
model = Letter
model.content_type = "application/pdf"
Letter
model itself, I included the following, also without any luck:在Letter
模型本身中,我包括以下内容,但也没有任何运气:@property
def content_type(self):
return "application/pdf"
-- --
Apologies on what I think might be a basic question.对我认为可能是一个基本问题的道歉。
Any pointers would be very welcomed.任何指针都会非常受欢迎。
It's perhaps just a case of me not being familiar with the right terminology in SQL Alchemy and missing the obvious.这可能只是我不熟悉 SQL Alchemy 中的正确术语并遗漏了显而易见的情况。
Does anyone understand my issue and have any threads they could give me to pull on?有没有人理解我的问题并有他们可以给我的线索?
This worked for me, I must have got something wrong initially:这对我有用,我最初一定有问题:
File(Base):文件(基础):
...
def __init__(self, **kwargs):
super().__init__(**kwargs)
...
Letter(File):信件(档案):
...
def __init__(self, **kwargs):
kwargs.setdefault("content_type", "application/pdf")
super().__init__(**kwargs)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.