Skip to content

Using ObjectField/NestedField- Empty array #13

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

Open
yarnball opened this issue Mar 30, 2017 · 1 comment
Open

Using ObjectField/NestedField- Empty array #13

yarnball opened this issue Mar 30, 2017 · 1 comment

Comments

@yarnball
Copy link

yarnball commented Mar 30, 2017

Hi,

I am trying to return data from a function, but the result is empty:

For example, the result is:

"name": "XYZ",
"tag" : [ { }, { } ],

But should be:

"name": "XYZ"
"tag": {1: [{'taglevel': 1, 'name': Foo'}], 2: [{'taglevel': 2, 'name': Bar'}}

I have tried both the NestedField and ObjectField options. Both gave the same traceback (as per below).

I used pdb to debug, and I can see the correct result at return grouped_tags but it never arrives in elasticsearch.

IF THIS ISN'T POSSIBLE PLEASE tell me as I have spent weeks on this, and raised issues here and StackOverflow.

Otherwise, how can I get the results from my function?

Thanks

documents.py

vehicle = Index('vehicle')

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

@vehicle.doc_type
class VehicalDocument(DocType):
    tag = fields.ObjectField(attr="get_grouped_tags")
    class Meta:
        model = Vehicle
        fields = [
            'name'
        ] 

models.py

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

class Vehicle(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
@yarnball yarnball changed the title Using Objectfield- VariableLookupError for self Using ObjectField/NestedField- Empty array Mar 30, 2017
@sabricot
Copy link
Member

sabricot commented Apr 7, 2017

You don't declare the mapping for the ObjectField but i guess it is because your mapping is kind of dynamic ? it can be one time {1: [{'taglevel': 1, 'name': Foo'}], 2: [{'taglevel': 2, 'name': Bar'}} another time {1: [{'taglevel': 1, 'name': Foo'}]} and so on ?

You need to declare the mapping of your data in ObjectField, but as your data is formated now, it will not work.
Can you put you data in a list of objects instead, like this ?
[{'find_name_1': 1, 'find_name_2': [{'taglevel': 1, 'name': Foo'}]}, {'find_name_1': 2', 'find_a_name_2': [{'taglevel': 2, 'name': Bar'}] } ]

Sorry for the dummy example i don't know how to call these fields :)
Then you need to declare the mapping in Objectfield.

ObjectField(attr="get_grouped_tags", properties={
        'field_name_1': fields.IntergerField(),
        'field_name_2': fields.ObjectField(properties={
                'name': fields.IntergerField(),
                'tagLevel': fields.StringField(),
        }),
    }
)

I haven't test theses examples it's just some indications.
You probably shouldn't use a small library, not release on pip and with not many contrib/stargazers, if you don't really know exactly what you want from it.

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