On 03/06/2017 04:16 PM, Leonardo Mata wrote:
> Hello, My applications does some ordering using the distance from
> latitude and longitude haversine distance, i was able to calculate this
> using @hybrid.method and @.*expression, but i can't output the
> calculated distance:
>
> /class PartnerAddress(db.Model, WithTimestampsModel, SerializeMixin):/
> / /
> / # Columns/
> / id = db.Column(db.Integer(), primary_key=True, nullable=False)/
> /
> /
> / partner_id = db.Column(/
> / db.Integer(),/
> / db.ForeignKey('
partner.id'),/
> / nullable=False/
> / )/
> /
> /
> / latitude = db.Column(db.Numeric(precision=9, scale=7), nullable=False)/
> / longitude = db.Column(db.Numeric(precision=10, scale=7),
> nullable=False)/
> / /
> /
> /
> / @hybrid_method/
> / def distance(self, lat, lng):/
> / /
> / return math.acos(math.cos(math.radians(self.latitude)) *
> math.cos(math.radians(lat)) */
> / math.cos(math.radians(self.longitude) -
> math.radians(lng)) +/
> / math.sin(math.radians(self.latitude)) *
> math.sin(math.radians(lat))) * 6371/
> /
> /
> / @distance.expression/
> / def distance(cls, lat, lng):/
> / return func.acos(func.cos(func.radians(cls.latitude)) *
> func.cos(func.radians(lat)) */
> / func.cos(func.radians(lng) -
> func.radians(cls.longitude)) +/
> / func.sin(func.radians(cls.latitude)) *
> func.sin(func.radians(lat))) * 6371/
> /
> /
> /
> /
> /class PartnerV3(db.Model, WithTimestampsModel,/
> / SoftDeletableModel, SerializeMixin):/
> / /
> /
> /
> / name = db.Column(db.String(128), nullable=False)/
> / /
> /
> /
> / address = db.relationship(/
> / 'mustafar.partner.v3.models.address.PartnerAddress',/
> / backref='partner',/
> / uselist=False/
> / )/
>
>
> when querying like this:
>
> query =
> PartnerV3.query.join(PartnerAddress).order_by(PartnerAddress.distance(lat,
> lng)).paginate(1,10, False)
>
> I can paginate the result, but it returns only the PartnerV3 object, i
> can't access the distance.
>
> How do I access this distance property?
you have a PartnerV3 there so the PartnerAddress is on the .address
property:
query =
PartnerV3.query.join(PartnerAddress).order_by(PartnerAddress.distance(lat,
lng))
some_partner = query.first()
partner_address = some_partner.address
distance = partner_address.distance(lat, lng)
>
>
>
>
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
>
http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See
http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
sqlalchemy+...@googlegroups.com
> <mailto:
sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to
sqlal...@googlegroups.com
> <mailto:
sqlal...@googlegroups.com>.
> Visit this group at
https://groups.google.com/group/sqlalchemy.
> For more options, visit
https://groups.google.com/d/optout.