Comments (2)
Good afternoon
Also faced with such a problem
If, when sampling from a model with a ForeignKey column, one record has a value, and the second one does not, then both values have None
And if both entries with ForeignKey were filled in, then everything will be fine.
I did a little research and realized that in django_pandas/utils.py
def replace_pk(model):
base_cache_key = get_base_cache_key(model)
def get_cache_key_from_pk(pk):
return None if pk is None else base_cache_key % str(pk)
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
def replace_pk(model):
base_cache_key = get_base_cache_key(model)
def get_cache_key_from_pk(pk):
return None if pk is None else base_cache_key % int(pk)
Dear developers, please check this and change it in new versions if this is the right solution
from django-pandas.
@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()
MyApp1.MyModel.MyModelId
<ManyToOneRel: MyApp1.myforeignkeymodel>
MyApp1.MyForeignKeyModel.Name
<ManyToOneRel: MyApp1.myforeignkeymodel>
FieldDoesNotExist => MyForeignKeyModel has no field named 'pk'
'Options' object has no attribute 'get_all_related_objects_with_model' :::: DEPRECATED FROM DJANGO 1.1
Django Docs for Deprecation
The result I get on calling the classmethod of MyModel is :
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
and the result on calling the classmethod of MyForeignKeyModel is :
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
2 of them (id 3 and 4) had null value for the Foreign Key
from django-pandas.
Related Issues (20)
- Cannot extend admin-tools blocks into new templates HOT 1
- Django 3.1: AttributeError: module 'django.db.models.fields' has no attribute 'FieldDoesNotExist' with annotation HOT 3
- Safely saving pandas DataFrame to SQL table via Django HOT 5
- Possible to release latest dev version to PyPI? HOT 8
- Bug: `read_frame` does not work if at least one manager is not named "objects"
- How can I merge different tables on the basis of a foreign key?
- error if primary_key is not int() HOT 6
- Converting django.models.DateField to datetime64 in read_frame
- test_io.RelatedFieldsTest.test_verbose: ValueError: cannot convert float NaN to integer HOT 6
- Race condition of queryset in read_frame HOT 3
- configure github actions (travis tests are no longer running) HOT 4
- run tests with warnings enabled HOT 1
- Stack Trace - ValueError: cannot convert float NaN to integer - When a foreign key is null HOT 4
- force_text removed in Django 4.0 HOT 2
- Bug with binary field and to_pivot_table HOT 2
- Data truncation
- read_frame breaks if fieldnames and column_names are passed and there is an FK reference in the fieldnames
- Pandas 2.1 has a deprecation warning for convert_dtype on read_frame HOT 7
- Feature proposal: Allow providing dicts to fieldnames for downstream renaming
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-pandas.