Foreign key problem

99 views
Skip to first unread message

Guy Nesher

unread,
Oct 14, 2011, 7:44:45 AM10/14/11
to Django users

Hi,

I'm trying to populate a table with 2 foreign keys using a csv file
and keep getting the following error :
Cannot assign "238": "PrizetoRestaurant.RestauranttId" must be a
"Restaurant" instance.

I tried to manually define the primary keys in those tables, and I'm
making sure I int() the data from the csv, but still now luck.
I've also looked in the db and a restaurant with id 238 does exist in
the Restaurant table.

I'm attaching the relevant code bellow.

Thanks!

I have the following model:

class Restaurant(models.Model):
Id = models.IntegerField(unique=True, primary_key=True)
Name = models.CharField(max_length=128)
Address = models.CharField(max_length=128)
Zip = models.CharField(max_length=128)
City = models.CharField(max_length=128)
Phone = models.CharField(max_length=128)
Latitude = models.DecimalField(max_digits=11, decimal_places=9)
Longitude = models.DecimalField(max_digits=11, decimal_places=9)

class Prize(models.Model):
Id = models.IntegerField(unique=True, primary_key=True)
Name = models.CharField(max_length=128)
GroupId = models.ForeignKey('PrizeGroup', to_field='Id')

class PrizetoRestaurant(models.Model):
RestaurantId = models.ForeignKey('Restaurant', to_field='Id')
PrizeId = models.ForeignKey('Prize', to_field='Id')
Date = models.DateField()
Quantity = models.IntegerField()


and the code is :


import csv
reader = csv.reader(open("/Users/guy.nesher/Work/tmsw/swiss/src/
monopoly/static/csv/dailydatatest.csv", "rU"), dialect='excel')
for row in reader:

PrizetoRestaurant.objects.get_or_create(RestaurantId=int(row[0]),
PrizeId=prizedict[2], Date = row[10], Quantity = row[2])

Daniel Roseman

unread,
Oct 14, 2011, 8:24:29 AM10/14/11
to django...@googlegroups.com
The error is quite clear: you're getting an integer from the CSV, but Django's ForeignKey expects an actual instance of the related table. If you'd called your field something sensible like "restaurant" this would be more obvious.

You can actually assign the ID directly by postfixing _id to the field name - so in your case, it would be RestaurantId_id

Please, also read PEP8 and don't give your fields InitialCaps names - they should be lower_case_with_underscore.
--
DR. 

Guy Nesher

unread,
Oct 14, 2011, 8:54:44 AM10/14/11
to Django users
Hi DR,

Thanks, I've just started developing in Python/Django so I do
apologize for the bad naming convention (I'll fix this once everything
works, don't want to add additional errors before I sort this).

Having said that I assumed that if I define the foreignkey manually
and specifically select an int field it will work.

I've tried to add the _id to the update process (ResturantId to
ResturantId_id) but I just get an error saying the field does not
exist :
Cannot resolve keyword 'RestaurantId_id' into field. Choices are:
Date, PrizeId, Quantity, RestaurantId, id

The new update line now looks like this:
PrizetoRestaurant.objects.get_or_create(RestaurantId_id=int(row[0]),
PrizeId=prizedict[2], Date = row[10], Quantity = row[2])

I'm guessing I'm missing something, just not sure what

Guy Nesher

unread,
Oct 14, 2011, 10:09:15 AM10/14/11
to Django users
nm, solved
Reply all
Reply to author
Forward
0 new messages