Skip to content

Accessing nested fields via a function (M2M) #11

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

Closed
yarnball opened this issue Mar 17, 2017 · 3 comments
Closed

Accessing nested fields via a function (M2M) #11

yarnball opened this issue Mar 17, 2017 · 3 comments

Comments

@yarnball
Copy link

yarnball commented Mar 17, 2017

Great work on this!

I'm having trouble with my nested data.

Due to my front end, my data has to stay in a this layout/=format. The data (what get_grouped_tags) returns will look like this:

    "tag" : {
      "1" : [ {
        "taglevel" : 1,
        "name" : "Foo"
      } ],
      "2" : [ {
        "taglevel" : 2,
        "name" : "Bazz"
      } ]
    },

I tried this, which tells me "illegal argument exceptions":

documents.py

from django_elasticsearch_dsl import DocType, Index, fields
from .models import Item

item = Index('items')

item.settings(
    number_of_shards=1,
    number_of_replicas=0
)

@item.doc_type
class ItemDocument(DocType):
    tag = fields.StringField(attr="get_grouped_tags")

    class Meta:
        model = Item
        fields = [
            'typetask',
            'title',
        ] 

models.py

class Tag(models.Model):
    name = models.CharField("Name", max_length=5000, blank=True)
    taglevel = models.IntegerField("Tag level", null=True, blank=True)

    def to_search(self):
        tags = self.id
        if tags:
            queryset = Item.objects.filter(tag=tags)
            for object in queryset:
                object.save()
        return queryset

class Item(models.Model):
    title = models.CharField("Title", max_length=10000, blank=True)
    tag = models.ManyToManyField('Tag', blank=True)

    def get_grouped_tags(self):
        tag = self.tag.order_by('taglevel')
        grouped_tags = {
            tag_level: [
                { 'name': tag_of_level.name, 'taglevel': tag_of_level.taglevel, }
                for tag_of_level in tags_of_level
            ] for tag_level, tags_of_level
            in groupby(tag, lambda tag: tag.taglevel)
        }
        return grouped_tags
  • I tried tag = fields.NestedField(attr="get_grouped_tags"), but the tags field just comes up empty in the index.

  • I tried:

    tag = fields.NestedField(properties={
        'name': fields.StringField(),
    })

But that returns this traceback gave be a keyError for manager (http://dpaste.com/35D46HP). Also, even if this did work, the data would still need to be presented as it looks in my function.

How do I properly access my nested data from an M2M field?

Thanks

PS:

Could be good to tell newbies how to install it pip install git+https://github.com/sabricot/django-elasticsearch-dsl.git

@yarnball yarnball changed the title Accessing nested fields via a function Accessing nested fields via a function (M2M) Mar 18, 2017
@yarnball
Copy link
Author

Bump- if anyone can advise please let me know

@yarnball
Copy link
Author

Please help me out- any user. I have tried so many ways without success

@yarnball
Copy link
Author

Opened new thread, more clear error example #13

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

1 participant