Greetings to everybody,
I have a problem for which I didn't find solution on internet,
therefore I am posting context:
simplified models.py for application testapp (django 1.3):
class Menu(models.Model):
parent_menu = models.ForeignKey('Menu', blank=True, null=True)
title = models.CharField(max_length=30)
def __unicode__(self):
return self.title
class Page(models.Model):
menu = models.ForeignKey('Menu', blank = True, null=True)
file admin.py
from testapp.models import Page, Menu
from django.contrib import admin
class PageAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "menu":
kwargs["queryset"] = Menu.objects.raw('select * from
testapp_menu where not id in (select distinct parent_menu_id from
testapp_menu where parent_menu_id is not null)')
kwargs["queryset"].all = kwargs["queryset"].__iter__
return super(PageAdmin,
self).formfield_for_foreignkey(db_field, request, **kwargs)
class MenuAdmin(admin.ModelAdmin):
pass
admin.site.register(Menu, MenuAdmin)
admin.site.register(Page, PageAdmin)
some explanation:
When I am creating Page, I want to have in a list all Menu instances
which either don't have parent menu or don't have "child
menu" (inverse relationship as defined in model). Now I didn't find
any suitable query using Django syntax, that's why I used raw query.
It filters Menu objects as I expect, but when I want to save Page
instance, I get following error 'RawQuerySet' object has no attribute
'get'
problem is in method \django\forms\models.py to_python
972. value = self.queryset.get(**{key: value})
I don't know how to bypass this error and I didn't find anywhere
discussion about this case, so every help and advice is much
appreciated.
zdenulo
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.