Foreign key problem

2011-10-14 Thread Guy Nesher

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Foreign key problem

2011-10-14 Thread Guy Nesher
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


On Oct 14, 1:24 pm, Daniel Roseman  wrote:
> On Friday, 14 October 2011 12:44:45 UTC+1, Guy Nesher wrote:
>
> > 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])
>
> 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Foreign key problem

2011-10-14 Thread Guy Nesher
nm, solved

On Oct 14, 1:54 pm, Guy Nesher  wrote:
> 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
>
> On Oct 14, 1:24 pm, Daniel Roseman  wrote:
>
>
>
>
>
>
>
> > On Friday, 14 October 2011 12:44:45 UTC+1, Guy Nesher wrote:
>
> > > 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])
>
> > 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Error handeling with Try

2011-10-19 Thread Guy Nesher
I have an odd problem tracking errors in a for loop
I assign a loop counter to each error and print them at the end of the
loop, but all the errors are numbered to the last iteration.

The code goes something like this :

error = [0,[]]
counter = 0

for row in reader:
  counter +=1
  try:
#something
  except Exception, e:
error[0] = counter
error[1] = e
errors.append (error)
return HttpResponse("%s" % errors)

What am I missing here ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Error handeling with Try

2011-10-19 Thread Guy Nesher
Thanks

On Oct 19, 3:57 pm, "J. Cliff Dyer"  wrote:
> On 10/19/2011 10:49 AM, Guy Nesher wrote:
>
>
>
>
>
>
>
>
>
> > I have an odd problem tracking errors in a for loop
> > I assign a loop counter to each error and print them at the end of the
> > loop, but all the errors are numbered to the last iteration.
>
> > The code goes something like this :
>
> > error = [0,[]]
> >      counter = 0
>
> > for row in reader:
> >    counter +=1
> >    try:
> >      #something
> >    except Exception, e:
> >      error[0] = counter
> >      error[1] = e
> >      errors.append (error)
> > return HttpResponse("%s" % errors)
>
> > What am I missing here ?
>
> You only have one list object, so you're changing the same list each
> time you hit your exception.
>
> What you want is more like:
>
>      errors = []
>      for row in reader:
>          counter += 1
>          try:
>              #something
>          except Exception, e:
>              error = [counter, e]
>              errors.append(error)
>      return HttpResponse("%s" % errors)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Piston XML Attributes

2011-10-23 Thread Guy Nesher
Hi,

I've started playing with Piston several days ago but I'm unable to
create XML attributes.
Do I need to create a special emitter for that ?

Guy

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Piston XML Attributes

2011-10-23 Thread Guy Nesher
Thanks

On Oct 23, 9:17 pm, Masklinn  wrote:
> On 2011-10-23, at 21:20 , Guy Nesher wrote:> Hi,
>
> > I've started playing with Piston several days ago but I'm unable to
> > create XML attributes.
> > Do I need to create a special emitter for that ?
>
> From what I can read in the XMLEmitter code, I would think so yes: Piston's 
> built-in XMLEmitter maps arbitrary resources to element trees, and does not 
> bother with attributes at all (I don't see how you could built a simple 
> generic emitter deciding whether to serialize a given dict to attributes or 
> to elements, that would at the very least require pre-checking all dicts to 
> check whether their values are all atomic, and due to the way SAX serializers 
> work it would have to be done before creating the parent node)
>
> If you want to generate attributes, you will have to create a custom emitter 
> indeed.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Passing variables from a context processor to a template

2012-01-05 Thread Guy Nesher
Hi,

I've created a simple context processor which simply returns a
variable, however I'm unable to retrieve it from my template.

The context processor is quite simple :

def swiss_context_processors(request):
mytest = "aaa"
return mytest

and I am calling the context processor in my view :

def index(request):
return render_to_response('front/
index.html',context_instance=RequestContext(request)
)

However when I try to access the variable in my template {{mytest}} it
returns empty.

What am I missing ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Passing variables from a context processor to a template

2012-01-05 Thread Guy Nesher
Thanks,

I've initially tried to use a dictionary but was still unable to pull
the data in the template.

I'm using your updated context processor:
def swiss_context_processors(request):
added_context = { 'mytest': 'aaa', }
return added_context

and trying to call {{added_context.mytest}} in the template which
returns nothing

I've added the context processor to settings.py and when I run a
pdb.trace() from the context_processor I am able to print the variable
so it is being called + defined.



On Jan 5, 5:22 pm, Nan  wrote:
> Two things:
>
> 1) make sure the context processor is installed in your settings file.
> 2) context processors should return dicts.  Try:
>
> def swiss_context_processors(request):
>     added_context = { 'mytest': 'aaa', }
>     return added_context
>
> On Jan 5, 12:03 pm, Guy Nesher  wrote:
>
>
>
>
>
>
>
> > Hi,
>
> > I've created a simple context processor which simply returns a
> > variable, however I'm unable to retrieve it from my template.
>
> > The context processor is quite simple :
>
> > def swiss_context_processors(request):
> >     mytest = "aaa"
> >     return mytest
>
> > and I am calling the context processor in my view :
>
> > def index(request):
> >     return render_to_response('front/
> > index.html',context_instance=RequestContext(request)
> >     )
>
> > However when I try to access the variable in my template {{mytest}} it
> > returns empty.
>
> > What am I missing ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Passing variables from a context processor to a template

2012-01-06 Thread Guy Nesher
Thanks,

Works perfectly now (and yeah I'm fairly new to Python/Django)

On Jan 5, 5:44 pm, Rainy  wrote:
> On Jan 5, 12:38 pm, Rainy  wrote:
>
>
>
>
>
>
>
>
>
> > On Jan 5, 12:35 pm, Guy Nesher  wrote:
>
> > > Thanks,
>
> > > I've initially tried to use a dictionary but was still unable to pull
> > > the data in the template.
>
> > > I'm using your updated context processor:
> > > def swiss_context_processors(request):
> > >     added_context = { 'mytest': 'aaa', }
> > >     return added_context
>
> > > and trying to call {{added_context.mytest}} in the template which
> > > returns nothing
>
> > It should be just {{ mytest }}
>
> I should add that in Python, it doesn't matter what is the
> variable name of value being returned. So if you have
>
> def x(): a=1; return a
>
> def y(): b=x()
>
> There is no way for y() to know that inside x(), the
> variable was called 'a'. It receives the value only. It's
> effectively equivalent to x() being:
>
> def x(): return 1
>
> So you need to understand that django template
> processing receives the dictionary {'mytest':'aaa'} and
> could not possibly know what you mean by 'added_context'.
>
>  -ak

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.