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...
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 a NotNullViolation
.
I have tried setting this within the Letter
model in a few different ways, but to no avail.
--
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.Letter
model, but this defeats Third Normal Form so I assume there is a better way to do it.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.content_type
on the model return in the FactoryBoy - LetterFactory
, again :class Meta(SQLAlchemyOptions):
model = Letter
model.content_type = "application/pdf"
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?
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)
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.