如何在表模型上设置默认值,该表模型将单独的表模型扩展为SQL Alchemy中的基础

发布于 2025-02-09 10:27:36 字数 2013 浏览 0 评论 0 原文

我在各种问题上进行了很好的挖掘,似乎缺少我觉得一定很明显的东西。

在Postgres中,使用python,sqlalchemy和Factoryboy进行运行(现在失败)测试,我有两个表模型...

  • 文件

letter 是实例化的 它扩展了 file

类字母(文件):

-

file 又扩展了SQL Alchemys dectarativeBase

class File (dectarativebase):

-

文件中,是一个称为 content_type 的属性。

content_type 现在已在文件上设置为不是可确定的

-

我需要做的事情感觉非常基本...

字母,我需要将默认的 content_type 设置为“应用程序” /pdf“ ... 就是这样!

,但对于我的所有尝试,我似乎都无法将其设置为 letter 的默认内容类型

这意味着我们已经运行的测试,无法尝试创建 file ,因为它没有 content_type 可以传递到 file> file ,这意味着我得到 notnullviolation

我尝试以几种不同的方式在字母模型中设置此设置,但无济于事。

-

方法:

  • 我尝试使用 __ INT __ file> file 的自定义基础模型中以及 inters> letter 中尝试使用。使用 kawgs 并尝试以这种方式设置它,尽管这不起作用。
  • 我已经考虑过将列添加到字母模型中,但是这会击败第三个正常形式,因此我认为有更好的方法可以做到这一点。
  • 我试图通过我们正在运行的测试中传递值,但是没有运气(测试使用 Factoryboy
  • 有一个 letterfactory (来自 Factory> Factoryboy )这是问题的来源,我尝试将 content_type =“ application/pdf” 添加到此中,而没有任何运气。
  • 我还尝试在 Factoryboy -LetterFactory 中再次设置 content_type ,再次:
class Meta(SQLAlchemyOptions):
    model = Letter
    model.content_type = "application/pdf"
  • Letter> Letter> Letter 模型本身中,我包括跟随,也没有任何运气:
@property
    def content_type(self):
        return "application/pdf"

-

对我认为可能是一个基本问题表示歉意。

任何指针都会非常受欢迎。

这也许只是我不熟悉SQL炼金术中正确的术语并缺少明显的情况。

有人了解我的问题,并有任何可以给我的线程吗?

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...

  • File
  • Letter

When Letter is instantiated, it extends File:

class Letter(File):

--

and File in turn extends SQL Alchemys DeclarativeBase:

class File(DeclarativeBase):

--

Within File, is a property called content_type.

content_type has now been set on File to be 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!

However, for all my trying, I cannot seem to get it to set this as the default content type for 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 aNotNullViolation.

I have tried setting this within the Letter model in a few different ways, but to no avail.

--

Approaches:

  • I tried using __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.
  • I have thought about adding the column into the Letter model, but this defeats Third Normal Form so I assume there is a better way to do it.
  • I have attempted to pass the value through the test we are running but without any luck (tests are using FactoryBoy)
  • There is a 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.
  • I have also attempt to set the content_type on the model return in the FactoryBoy - LetterFactory, again :
class Meta(SQLAlchemyOptions):
    model = Letter
    model.content_type = "application/pdf"
  • Within the Letter model itself, I included the following, also without any luck:
@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.

Does anyone understand my issue and have any threads they could give me to pull on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

━╋う一瞬間旳綻放 2025-02-16 10:27:36

这对我有用,最初我一定有一些错误:

文件(base):

...
def __init__(self, **kwargs):
    super().__init__(**kwargs)
...

letter(file):

...
def __init__(self, **kwargs):
    kwargs.setdefault("content_type", "application/pdf")
    super().__init__(**kwargs)
...

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)
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文