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

error if primary_key is not int() #134

Open
tlk3 opened this issue Jun 5, 2021 · 6 comments
Open

error if primary_key is not int() #134

tlk3 opened this issue Jun 5, 2021 · 6 comments

Comments

@tlk3
Copy link

tlk3 commented Jun 5, 2021


class Testing1(models.Model):
    symbol = models.CharField('Ticker', max_length=32, primary_key=True)
    tf = models.CharField('timeframe', max_length=1)


class Testing2(models.Model):
    symbol = models.ForeignKey(Testing1, on_delete=models.CASCADE)
    price = models.CharField('price', max_length=1)


t = Testing1.objects.create(symbol='AAPL', tf='5')
t
Out[6]: <Testing1: Testing1 object (AAPL)>

Testing2.objects.create(symbol=t, price='1')
Out[7]: <Testing2: Testing2 object (1)>

from django_pandas.io import read_frame
qs = Testing2.objects.all()
df = read_frame(qs)

Traceback (most recent call last):
  File "/Users/TLK3/PycharmProjects/stratbot/venv/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-9-b1349dc6e37f>", line 1, in <module>
    df = read_frame(qs)
  File "/Users/TLK3/PycharmProjects/stratbot/venv/lib/python3.8/site-packages/django_pandas/io.py", line 136, in read_frame
    update_with_verbose(df, fieldnames, fields)
  File "/Users/TLK3/PycharmProjects/stratbot/venv/lib/python3.8/site-packages/django_pandas/utils.py", line 83, in update_with_verbose
    df[fieldname] = function(df[fieldname])
  File "/Users/TLK3/PycharmProjects/stratbot/venv/lib/python3.8/site-packages/django_pandas/utils.py", line 46, in inner
    cache_keys = pk_series.apply(
  File "/Users/TLK3/PycharmProjects/stratbot/venv/lib/python3.8/site-packages/pandas/core/series.py", line 4138, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/lib.pyx", line 2467, in pandas._libs.lib.map_infer
  File "/Users/TLK3/PycharmProjects/stratbot/venv/lib/python3.8/site-packages/django_pandas/utils.py", line 42, in get_cache_key_from_pk
    return None if pk is None else base_cache_key % int(pk)
ValueError: invalid literal for int() with base 10: 'AAPL'

@abdeltif-b
Copy link

I have the same issue when the foreign-key in a model is a string. Any workaround for this?

@bartpijn
Copy link

I worked around it by specifying the cols.

@krishnakafle
Copy link

krishnakafle commented Mar 3, 2022

I have face similar issue

invalid literal for int() with base 10: 'bk-dw-02'

Specifying of columns doesn't help me either.

qs = GwMonitoringKobo.objects.all()
df = read_frame(qs, fieldnames=['date', 'district', 'latitude','longitude','altitude','precision','well_type','measurement_point_cm', 'measurement_of_wet_point_on_tape_in_m_field',
                 'gw_level_from_mp','mp_in_m','gw_level', 'fid','well_num'])

@sundyloveme
Copy link
Contributor

why I am ok??
I am configuration:
Django 2.2
numpy 1.21.5
pandas 1.3.5
pip 21.1.2
python-dateutil 2.8.2
pytz 2021.3
semver 2.13.0
setuptools 57.0.0
six 1.16.0
sqlparse 0.4.2
wheel 0.36.2

image

@TheAbhilash23
Copy link

Can you tell why you have set your entity relationship as

class Testing1(models.Model):
symbol = models.CharField('Ticker', max_length=32, primary_key=True)
tf = models.CharField('timeframe', max_length=1)

class Testing2(models.Model):
symbol = models.ForeignKey(Testing1, on_delete=models.CASCADE)
price = models.CharField('price', max_length=1)

When I setup the models I use Model Class as Foreign key, for instance

class Testing1(models.Model):
symbol = models.CharField('Ticker', max_length=32, primary_key=True)
tf = models.CharField('timeframe', max_length=1)

class Testing2(models.Model):
Testing1 = models.ForeignKey(Testing1, on_delete=models.CASCADE, related_name='Testing1Symbols')
price = models.CharField('price', max_length=1)

@tlk3
Copy link
Author

tlk3 commented Apr 28, 2023

Why would you code it this way? Never seen this type of model / ForeignKey relationship. It's not logical.

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

6 participants