django-pandas icon indicating copy to clipboard operation
django-pandas copied to clipboard

FK ID field is not properly converted

Open dmitry-saritasa opened this issue 6 years ago • 1 comments

When I'm instructing queryset to be converted to Pandas dataframe with

to_dataframe(['id', 'employee', 'employee_id'],index='job_date')

In df's column employee_id I have the same value as employee.


In [97]: df = Job.objects.all().order_by('-id')[:100].to_dataframe(['id', 'employee', 'employee_id'],index='job_date')

In [98]: df
Out[98]: 
                id   employee       employee_id
job_date                                                                        
2017-07-18  918187   [email protected]      [email protected]
2017-07-18  918186   [email protected]      [email protected]
2017-07-18  918185   [email protected]     [email protected]
2017-07-18  918184   [email protected]      [email protected]

In [100]: Job.objects.last().employee
Out[100]: <Employee: [email protected]>

In [101]: Job.objects.last().employee_id
Out[101]: 115

I have a str method in Job class

class Job(SyncTimeStampedModel, BaseModel):
    employee = models.ForeignKey(
        to='users.Employee',
        on_delete=models.PROTECT,
        related_name='jobs',
        verbose_name=_('Employee'),
    )
class Employee(TimeStampedModel, BaseModel):
    def __str__(self):
        return self.user.username

Question

So how to export the ID into dataframe?

dmitry-saritasa avatar Oct 16 '18 21:10 dmitry-saritasa

I have the same problem. The workaround I'm using is employee__id instead of employee_id

mick88 avatar Nov 18 '21 12:11 mick88