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

error if primary_key is not int()

Open tlk3 opened this issue 4 years ago • 6 comments


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'

tlk3 avatar Jun 05 '21 04:06 tlk3

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

abdeltif-b avatar Jul 06 '21 20:07 abdeltif-b

I worked around it by specifying the cols.

bartpijn avatar Jul 14 '21 19:07 bartpijn

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'])

krishnakafle avatar Mar 03 '22 06:03 krishnakafle

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

sundyloveme avatar Mar 12 '22 09:03 sundyloveme

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)

officialabhilash avatar Apr 28 '23 05:04 officialabhilash

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

tlk3 avatar Apr 28 '23 19:04 tlk3