Re: model can return a count but no data

2012-03-18 Thread Kevin Wetzels
 
On Sunday, March 18, 2012 12:55:31 PM UTC+1, Maarten Japink wrote:
>
> I 'm working on a taxonomie. 
> These are my models: 
>
> class Classificatie(models.Model): 
> name=models.CharField(max_length=60) 
>
> def __unicode__(self): 
> return self.name 
>
> class Taxonomie(models.Model): 
> name=models.CharField(max_length=60) 
> level=models.ForeignKey(Classificatie) 
> parent=models.ForeignKey('self', related_name='parent_id' ) 
> synoniem=models.ForeignKey('self', 
> related_name='synoniem_id' ,blank=True, null=True) 
>
> I populated the tabels in postgresql 
>
> When I do: 
> >>>Taxonomie.objects.count() 
> I get the proper number of rows (593) 
>
> but when i want to read a single object with: 
> >>> t=Taxonomie.objects.get(id=20) 
> Traceback (most recent call last): 
>   File "", line 1, in  
>   File "C:\Python27\lib\site-packages\django\db\models\manager.py", 
> line 132, in 
>  get 
> return self.get_query_set().get(*args, **kwargs) 
>   File "C:\Python27\lib\site-packages\django\db\models\query.py", line 
> 344, in g 
> et 
> num = len(clone) 
>   File "C:\Python27\lib\site-packages\django\db\models\query.py", line 
> 82, in __ 
> len__ 
> self._result_cache = list(self.iterator()) 
>   File "C:\Python27\lib\site-packages\django\db\models\query.py", line 
> 286, in i 
> terator 
> obj = model(*row[index_start:aggregate_start]) 
>   File "C:\Python27\lib\site-packages\django\db\models\base.py", line 
> 297, in __ 
> init__ 
> setattr(self, field.attname, val) 
>   File "C:\Python27\lib\site-packages\django\db\models\fields 
> \related.py", line 
> 402, in __set__ 
> manager.add(*value) 
> TypeError: add() argument after * must be a sequence, not int 
>
>
> I also tried the Taxonomie model with both ' self ' Foreignkey set to 
> blank=True and null=True 
> gives me the same problem. I also can't make new taxonomie objects 
> with django 
>
>
> What am I doiing wrong? 
>
>
You're using the implicit column_name as the related_name on the 
self-referencing FKs. Django will generate a column name from the name of 
your field, which means parent_id and synoniem_id columns will be used. 
Then you decide to use those as the related_name for the corresponding 
ForeignKey. This won't cause a problem when you perform a count since in 
that case no model instances need to be loaded; it's simply a query that's 
executed.

This works and is more inline with what you should expect of a related name:

class Taxonomie(models.Model):
name=models.CharField(max_length=60)
level=models.ForeignKey(Classificatie)
parent=models.ForeignKey('self', related_name='parents' ,blank=True, 
null=True)
synoniem=models.ForeignKey('self', related_name='synoniemen' 
,blank=True, null=True)  

By the way: if you can, you're better off using English for your code 
rather than Dutch.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/Icc1RBqovDcJ.
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: In views.py snippet of Code

2012-03-15 Thread Kevin Wetzels


On Thursday, March 15, 2012 7:32:53 PM UTC+1, Django_for_SB wrote:
>
> Hi All, 
>
> I'm using render_to_response() in my views.py module. And of course, 
> I've imported the method: 
>
> "from django.shortcuts import render_to_response" 
>
> # This is the code that's generating the error: 
> def hours_ahead(request, offset): 
> try: 
> offset = int(offset) 
> except ValueError: 
> raise Http404() 
> dt = datetime.datetime.now() + datetime.timedelta(hours = 
> offset) 
> return render_to_response('hours_ahead.html', {'hour_offset': 
> offset}, {'next_time': dt}) 
>
>
> Django is complaining that my "render_to_response()" statement is 
> wrong with the following debug statement: 
>
> "pop expected at least 1 arguments, got 0" 
>
> Here is the full traceback: 
>
>
> Traceback: 
> File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in 
> get_response 
>   111. response = callback(request, 
> *callback_args, **callback_kwargs) 
> File "C:\Python27\my_Djando_projects\HelloWorld\..\HelloWorld 
> \views.py" in hours_ahead 
>   31. return render_to_response('hours_ahead.html', 
> {'hour_offset': offset}, {'next_time': dt}) 
> File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in 
> render_to_response 
>   20. return HttpResponse(loader.render_to_string(*args, 
> **kwargs), **httpresponse_kwargs) 
> File "C:\Python27\lib\site-packages\django\template\loader.py" in 
> render_to_string 
>   190. context_instance.pop() 
>
> Exception Type: TypeError at /time/plus/3/ 
> Exception Value: pop expected at least 1 arguments, got 0 
>
>
>
>
>
>
> Since I'm still fairly new to Django, I can't quite see what I'm doing 
> incorrectly here. Any assistance would be greatly appreciated. Thank 
> you. 
>
> Best, 
>
> SB


You're passing in three parameters to render_to_response. The third one is 
expected to be an instance of Context (see 
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render_to_response),
 
but you're passing in a dictionary.

render_to_response('hours_ahead.html', {'hour_offset': offset, 'next_time': 
dt}) is what you want.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/2Oq52gruh2cJ.
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.