Code Monkey home page Code Monkey logo

Comments (6)

roadsideseb avatar roadsideseb commented on August 18, 2024

I am running into the same issue with using a ParentalKey in the ordering setting:

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 __gt__ and __lt__ on the Vineyard model to make them comparable. Something like this works for me:

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?

from django-modelcluster.

thenewguy avatar thenewguy commented on August 18, 2024

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:

class TagFilter(models.Model):
    class Meta:
        ordering = ['tag__slug']

    tag = models.ForeignKey(Tag, related_name="+")
    page = ParentalKey(TagFilterPage, related_name='tag_filters')

from django-modelcluster.

thenewguy avatar thenewguy commented on August 18, 2024

Btw, my workaround for the meantime is just to duplicate the data. Like this:

class Meta:
        ordering = ['_duplicate_tag_slug']

_duplicate_tag_slug = models.SlugField(max_length=100, editable=False)

And then set the data when tags are saved or the tagfilter is saved. Works easily enough with signals for simple ordering.

from django-modelcluster.

przemub avatar przemub commented on August 18, 2024

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"]

from django-modelcluster.

gasman avatar gasman commented on August 18, 2024

This has now been implemented in #148 (but isn't in a release yet, so you'll need to install directly from git).

from django-modelcluster.

przemub avatar przemub commented on August 18, 2024

@gasman Awesome, thank you a lot for letting me know!

from django-modelcluster.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.