Skip to content
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

ManyToOneRel can't be serialized #32

Open
skabbit opened this issue Jan 16, 2016 · 6 comments
Open

ManyToOneRel can't be serialized #32

skabbit opened this issue Jan 16, 2016 · 6 comments

Comments

@skabbit
Copy link

skabbit commented Jan 16, 2016

ManyToManyField with 'through' Model bring us to some problems

Error on line 118 in django_elasticsearch/serializers.py:
AttributeError: 'ManyToOneRel' object has no attribute 'rel'

@lauxley
Copy link
Contributor

lauxley commented Jan 26, 2016

Hi, I added a use case in the test model here but can't replicate, do you have more informations ? django version, models used, any customizations of the serializer ?
Note that in the meantime you can create a custom serializer for this field.

@skabbit
Copy link
Author

skabbit commented Jan 29, 2016

My apologies, I've got this problem on serializing ManyToOne relation like this:

class Agency(EsIndexable, models.Model):
    name = models.CharField(
        max_length=300,
        verbose_name='Agency name',
        blank=True, null=True, default=None)

    class Elasticsearch(EsIndexable.Elasticsearch):
        serializer_class = AgencyEsSerializer
        fields = [
            'id',
            'name',
            'oldnames',
        ]


class AgencyOldName(models.Model):
    agency = models.ForeignKey(
        Agency,
        related_name='oldnames',
        on_delete=models.SET_NULL,
        blank=True, null=True, default=None)
    name = models.CharField(
        max_length=300,
        verbose_name='Old agency name',
        blank=True, null=True, default=None)

And yes, I'm using custom serializer to solve this problem.

@lauxley
Copy link
Contributor

lauxley commented Feb 2, 2016

Hi, so the problem is that in the serializer for some reason 'field' is not an instance of Field but ManyToOneRel which is not a subclass of Field.
Can you show me the query(ies) and the serializer ?

@skabbit
Copy link
Author

skabbit commented Feb 3, 2016

Here is the AgencyEsSerializer, with custom serializer for this field:

class AgencyEsSerializer(EsJsonSerializer):
    """
    Agency serializer
    """

    def serialize_oldnames(self, instance, field_name):
        return list(instance.oldnames.all().values('name'))

And here is the code, causing the problem:

            Agency.es.reindex_all()

@lauxley
Copy link
Contributor

lauxley commented Feb 4, 2016

Hi, the problem is that reverse relationships are not implemented, it's a big blunder so thx for pointing it out. I started a PR it should solve your problem but it will requires more work before i can merge it.

@skabbit
Copy link
Author

skabbit commented Feb 5, 2016

Tnx a lot for this patch and for whole module of cause! 👍
I hope soon I'll have a time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants