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

Unable to get related model data in dump_bulk method #221

Closed
jatindergit opened this issue Apr 27, 2021 · 2 comments
Closed

Unable to get related model data in dump_bulk method #221

jatindergit opened this issue Apr 27, 2021 · 2 comments
Labels

Comments

@jatindergit
Copy link

jatindergit commented Apr 27, 2021

Hi
I have following models:

class ClientLevel(TimeStampModel):
    name = CICharField(_('Name'), max_length=50)
    client = models.ForeignKey(Client, related_name='client_levels', on_delete=models.CASCADE)
    order = models.PositiveSmallIntegerField(_('Display Order'))`


class ClientDivision(AL_Node, TimeStampModel):
    name = models.CharField(max_length=50)
    parent = models.ForeignKey('self', related_name='children_set', null=True, db_index=True,
                               on_delete=models.DO_NOTHING)
    level = models.ForeignKey(ClientLevel, on_delete=models.PROTECT, related_name='client_level_divisions')
    client = models.ForeignKey(Client, related_name='client_divisions', on_delete=models.CASCADE)
    node_order_by = ['name']

Issue is, I want to fetch level__order field value in dump_bulk method. How can I do this. Please help

@agualdron
Copy link

I have the same issue.

With the following models:

class Customer(models.Model):
    email = models.EmailField(unique=True)

class BaseAsset(models.Model):
    name = models.CharField(max_length=INLINE_MAX_LENGTH, blank=True, default="")
    type = models.CharField(max_length=INLINE_MAX_LENGTH, blank=True, default="")
    description = models.TextField(blank=True, default='')


class Asset(MP_Node, BaseAsset):
    customer = models.ForeignKey('Customer', null=True, blank=True, on_delete=models.SET_NULL)

the .dump_bulk() function returns this:

[
    {
        "data": {
            "customer": 1
        },
        "baseasset_ptr_id": 1,
        "children": [
            {
                "data": {
                    "customer": 1
                },
                "baseasset_ptr_id": 4,
                "children": [
                    {
                        "data": {
                            "customer": 1
                        },
                        "baseasset_ptr_id": 6
                    },
                    {
                        "data": {
                            "customer": 1
                        },
                        "baseasset_ptr_id": 7
                    }
                ]
            },
            {
                "data": {
                    "customer": 1
                },
                "baseasset_ptr_id": 5
            }
        ]
    },
    {
        "data": {
            "customer": 1
        },
        "baseasset_ptr_id": 8
    },
    {
        "data": {
            "customer": null
        },
        "baseasset_ptr_id": 9
    }
]

And the admin view shows this structure:
image

@solarissmoke
Copy link
Collaborator

dump_bulk just uses Django's built-in serializers to serialize your model data - which will just provide the ID of related objects by default. Your use case is very specific, so will have to do a bit of work to handle it. Options include:

  • Post-process the generated output to add additional data (order in this case), by fetching it using the ID that has already been exported. This is probably the easiest and should be relatively straightforward to do.
  • Provide your own python serializer which does what you need.
  • Use a different utility (e.g., django rest framework) which provides more flexible serialization tools.

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

No branches or pull requests

3 participants