New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sort_by_fields() fails when model is ordered through a foreign key #45
Comments
I am running into the same issue with using a class Vineyard(Model):
name = CharField(max_length=200)
class Dinner(ClusterableModel):
vineyard = ParentalKey(Vineyard)
class Meta:
ordering = ('vineyard',) This raises an exception with the following message: File "$HOME/lib/python3.5/site-packages/modelcluster/utils.py", line 19, in sort_by_fields
items.sort(key=lambda x: (getattr(x, key) is not None, getattr(x, key)), reverse=reverse)
TypeError: unorderable types: Vineyard() < Vineyard() One way to make this work is to implement class Vineyard(Model):
name = CharField(max_length=200)
def __lt__(self, other):
return self.name < other.name
def __gt__(self, other):
return self.name > other.name I'm not quite sure if that would be the suggested solution or more of a workaround. I would think an implementation that works with Django model out of the box would be best but implicitly comparing model without adding additional DB queries might not be straight forward. Are there any alternative suggestions? |
I've encountered this as well attempting to order based on a field of the foreign key relation as well. My use case is attempting to use the InlinePanel on a "through" model to mimic a m2m relation. My intentions are to order based on a tag:
|
Btw, my workaround for the meantime is just to duplicate the data. Like this:
And then set the data when tags are saved or the tagfilter is saved. Works easily enough with signals for simple ordering. |
This is a fix for job listings that have multiple USAJobs links in them. With Python 3's stricter comparisons, attempting to order the `USAJobsApplicationLink` objects using `applicant_type` was failing with `ApplicantType` not supporting `<` operations. This fix adds `__gt__` and `__lt__` to `ApplicantType` to support `>` and `<` operations. Generally this appears to come from django-modelcluster and behavior that differs from the Django expectation, possibly related to wagtail/django-modelcluster#45.
The problem is still there. My workaround without duplicating the data is: class Category(models.Model):
name = models.CharField(unique=True, blank=False, max_length=50)
type = models.ForeignKey(
CategoryType, null=False, on_delete=models.CASCADE
)
@property
def type__name(self):
"""
Circumvents https://github.com/wagtail/django-modelcluster/issues/45
"""
return self.type.name
class Meta:
ordering = ["type__name", "name"] |
This has now been implemented in #148 (but isn't in a release yet, so you'll need to install directly from git). |
@gasman Awesome, thank you a lot for letting me know! |
If the
ordering
field of a model is configured to order the objects over a foreign key relationship (using the__
syntax), sort_by_fields() fails.E.g.
The text was updated successfully, but these errors were encountered: