-
Notifications
You must be signed in to change notification settings - Fork 118
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
read_frame bug: ForeignKey lookup fails if any Null values present #128
Comments
@odoublewen Please let me know if the following work around seems useful to you. MyModel (parent) in models.py class MyModel(models.Model): MyModelId = models.BigAutoField( _('Id'), primary_key=True ) Name = models.CharField( _('Name'), max_length=225, null=True, blank=True ) Date = models.DateField( _('Date'), null=True, blank=True ) DateTime = models.DateTimeField( _('DateTime'), null=True, blank=True ) Integer = models.IntegerField( _('Integer'), null=True, blank=True ) Float = models.FloatField( _('Float'), null=True, blank=True, ) def __str__(self): return f'{self.pk} - {self.Name}' class Meta: db_table = 'MyModel' verbose_name = 'My Model Object' verbose_name_plural = 'My Model Objects' @classmethod def get_dataframe(cls, instance=None): if not instance: qs = cls.objects.all() return read_frame(qs, ('MyModelId', 'MyForeignKeyModels__Name', 'MyForeignKeyModels__pk', 'Name', 'Date', 'DateTime', 'Integer', 'Float',)) MyForeignKeyModel is defined as class MyForeignKeyModel(models.Model): MyForeignKeyModelId = models.BigAutoField( _('Id'), primary_key=True ) MyModel = models.ForeignKey( 'MyModel', on_delete=models.CASCADE, related_name='MyForeignKeyModels', null=True, blank=True ) Name = models.CharField( _('Name'), max_length=225, null=True, blank=True ) Date = models.DateField( _('Date'), null=True, blank=True ) DateTime = models.DateTimeField( _('DateTime'), null=True, blank=True ) Integer = models.IntegerField( _('Integer'), null=True, blank=True ) Float = models.FloatField( _('Float'), null=True, blank=True, ) def __str__(self): return f'{self.pk} - {self.Name}' @classmethod def get_dataframe(cls, instance=None, *args, **kwargs): if not instance: qs = cls.objects.all() fields_list = [] for field in cls._meta.fields: import ipdb ipdb.set_trace() fields_list.append(field.name) return read_frame(qs, ('MyForeignKeyModelId', 'Name', 'Date', 'DateTime', 'Integer', 'Float', 'MyModel__Name')) class Meta: db_table = 'MyForeignKeyModel' verbose_name = 'My Foreign Key Model' verbose_name_plural = 'My Foreign Key Models' In [1]: from MyApp1.models import * In [2]: df = MyModel.get_dataframe() FieldDoesNotExist => MyForeignKeyModel has no field named 'pk'
MyModelId MyForeignKeyModels__Name MyForeignKeyModels__pk Name Date DateTime Integer Float 0 1 huyjgjhm 1 masdfasdf 2023-04-28 2023-04-28 07:14:16+00:00 4 3.122 1 1 bvnbvnvb 2 masdfasdf 2023-04-28 2023-04-28 07:14:16+00:00 4 3.122 2 2 321321 5 ppolkjm 2023-04-28 NaT 3423 123.000 3 2 ij9045 6 ppolkjm 2023-04-28 NaT 3423 123.000
In [3]: df Out[3]: MyForeignKeyModelId Name Date DateTime Integer Float MyModel__Name 0 1 huyjgjhm 2023-04-28 2023-04-28 07:14:28+00:00 NaN NaN masdfasdf 1 2 bvnbvnvb 2023-04-28 NaT 45.0 34.000 masdfasdf 2 3 sdfgdsfg 2023-04-28 2023-04-28 07:15:10+00:00 333.0 2342.000 None 3 4 gfhju767 2023-04-28 2023-04-28 07:15:31+00:00 12323.0 NaN None 4 5 321321 2023-04-28 2023-04-28 07:16:06+00:00 NaN 112.000 ppolkjm 5 6 ij9045 2023-04-28 NaT 7744.0 1.025 ppolkjm NOTE: There were 2 objects of MyModel and 6 objects of MyForeignKetModel |
Good afternoon I did a little research and realized that in django_pandas/utils.py
and if one of the ForeignKey records arrives empty, then in the get_cache_key_from_pk function, the pk parameter comes as float for all filled records and NoneType for non-filled ones, and if all records have a ForeignKey other than Null, then pk comes as int. Well, from here, after executing the get_cache_key_from_pk function for pk with the float type, it adds '.0' to the identifier, which is then not found. if you change instead to int(pk), then everything works
Dear developers, please check this and change it in new versions if this is the right solution |
ForeignKey lookup works as long as no null values are present. But I also have some models where null values are allowable, for example:
read_frame
returns expected results as long as all of the qs objects are non null for fieldfoo
:...but if one null is present, all rows in the df become Null.
Somewhat similar to #93 ?
The text was updated successfully, but these errors were encountered: