#31967: Resolving an output_field for mixed types crashes in database functions. -------------------------------------+------------------------------------- Reporter: Matt Hegarty | Owner: Hasan | Ramezani Type: Bug | Status: closed Component: Documentation | Version: dev Severity: Normal | Resolution: fixed Keywords: | Triage Stage: Ready for | checkin Has patch: 1 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by retpolanne): Hello! I see that this is fixed, but I still don't understand how to workaround this issue. I'm working on updating a Django app using this library: https://github.com/annemacedo-tw/django-spillway I get this error when using it: {{{ django.core.exceptions.FieldError: Expression contains mixed types: MultiPolygonField, FloatField. You must set output_field. }}} However I don't get the same error on Django 3.1 As far as I debugged, this line here yields these two fields https://github.com/annemacedo-tw/django- spillway/blob/fa008ef0db5f3434c1ef438f66231f35a90b1e6d/spillway/query.py#L153 {{{ SimplifyPreserveTopology(F(geometry), Value(0.0439453125)) }}} The first one being a MultiPolygonField and the other one being a FloatField. I tried setting the output_field and recreating the source_expressions but it didn't really work. {{{ sql = SimplifyPreserveTopology(sql, tilew) sql_field = sql.source_expressions[0] sql_value = sql.source_expressions[1] sql_value.output_field = models.MultiPolygonField() sql.set_source_expressions([sql_field, sql_value]) }}} (I understand that I'm setting an output field for MultiPolygonField here instead of FloatField as should be expected, I set it up to avoid the mixed types error) {{{ File "/usr/local/lib/python3.9/site- packages/django/contrib/gis/db/models/fields.py", line 194, in get_prep_value raise ValueError('Cannot use object with type %s for a spatial lookup parameter.' % type(obj).__name__) ValueError: Cannot use object with type float for a spatial lookup parameter. }}} TL;DR how can I work around this issue when using GeoDjango? Another thing I noticed is that ever since GeoManager was deprecated way back in 1.10, django.contrib.gis.db.models.FloatField resolves to django.db.models.fields.FloatField. I wonder if the gis FloatField would play nicely with MultiPolygonField. {{{ (Pdb) from django.contrib.gis.db import models (Pdb) models.FloatField <class 'django.db.models.fields.FloatField'> }}} I wish I could open a separate ticket for this, but I'm working on legacy code that I don't understand so well, so I don't know how to do a minimal version out of it. -- Ticket URL: <https://code.djangoproject.com/ticket/31967#comment:13> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018aad698149-c724498e-8064-457e-a88a-13856949cb0f-000000%40eu-central-1.amazonses.com.