Does Django ORM have an equivalent to SQLAlchemys Hybrid Attribute?

2024/10/1 12:17:22

In SQLAlchemy, a hybrid attribute is either a property or method applied to an ORM-mapped class,

class Interval(Base):__tablename__ = 'interval'id = Column(Integer, primary_key=True)start = Column(Integer, nullable=False)end = Column(Integer, nullable=False)def __init__(self, start, end):self.start = startself.end = end@hybrid_propertydef length(self):return self.end - self.start@hybrid_methoddef contains(self,point):return (self.start <= point) & (point < self.end)@hybrid_methoddef intersects(self, other):return self.contains(other.start) | self.contains(other.end)

This allows for distinct behaviors at the class and instance levels, thus making it simpler to evaluate SQL statements using the same code,

>>> i1 = Interval(5, 10)
>>> i1.length
5>>> print Session().query(Interval).filter(Interval.length > 10)
SELECT interval.id AS interval_id, interval.start AS interval_start,
interval."end" AS interval_end
FROM interval
WHERE interval."end" - interval.start > :param_1

Now in Django, if I have a property on a model,

class Person(models.Model):first_name = models.CharField(max_length=50)last_name = models.CharField(max_length=50)def _get_full_name(self):"Returns the person's full name."return '%s %s' % (self.first_name, self.last_name)full_name = property(_get_full_name)

It is my understanding that I can not do the following,

Person.objects.filter(full_name="John Cadengo")

Is there an equivalent of SQLAlchemy's hybrid attribute in Django? If not, is there perhaps a common workaround?

Answer

You're right that you cannot apply django queryset filter based on python properties, because filter operates on a database level. It seems that there's no equivalent of SQLAlchemy's hybrid attributes in Django.

Please, take a look here and here, may be it'll help you to find a workaround. But, I think there is no generic solution.

https://en.xdnf.cn/q/70960.html

Related Q&A

Building a Python shared object binding with cmake, which depends upon external libraries

We have a c file called dbookpy.c, which will provide a Python binding some C functions.Next we decided to build a proper .so with cmake, but it seems we are doing something wrong with regards to linki…

What linux distro is better suited for Python web development?

Which linux distro is better suited for Python web development?Background:I currently develop on Windows and its fine, but I am looking to move my core Python development to Linux. Im sure most any di…

Relation between 2D KDE bandwidth in sklearn vs bandwidth in scipy

Im attempting to compare the performance of sklearn.neighbors.KernelDensity versus scipy.stats.gaussian_kde for a two dimensional array.From this article I see that the bandwidths (bw) are treated diff…

How to style (rich text) in QListWidgetItem and QCombobox items? (PyQt/PySide)

I have found similar questions being asked, but without answers or where the answer is an alternative solution.I need to create a breadcrumb trail in both QComboBoxes and QListWidgets (in PySide), and …

Reconnecting to device with pySerial

I am currently having a problem with the pySerial module in Python. My problem relates to connecting and disconnecting to a device. I can successfully connect to my device and communicate with it for a…

How to check if a sentence is a question with spacy?

I am using spacy library to build a chat bot. How do I check if a document is a question with a certain confidence? I know how to do relevance, but not sure how to filter statements from questions.I a…

Python: Lifetime of module-global variables

I have a shared resource with high initialisation cost and thus I want to access it across the system (its used for some instrumentation basically, so has to be light weight). So I created a module man…

Power spectrum with Cython

I am trying to optimize my code with Cython. It is doing a a power spectrum, not using FFT, because this is what we were told to do in class. Ive tried to write to code in Cython, but do not see any di…

Detect abbreviations in the text in python

I want to find abbreviations in the text and remove it. What I am currently doing is identifying consecutive capital letters and remove them.But I see that it does not remove abbreviations such as MOOC…

filtering of tweets received from statuses/filter (streaming API)

I have N different keywords that i am tracking (for sake of simplicity, let N=3). So in GET statuses/filter, I will give 3 keywords in the "track" argument.Now the tweets that i will be recei…