On 14 Jul 2011 11:41, "Tom Evans" <[email protected]> wrote:
>
> On Thu, Jul 14, 2011 at 10:56 AM, zdenulo <[email protected]> wrote:
> > 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)
>
> ForeignKeys to themselves should use 'self' as the target model, eg:
>
> parent_menu = models.ForeignKey('self', 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)
>
>
> That just made me weep.
Lmao. That was just the laugh I needed to wake up to :P Kudos to the OP for
attempting this though :)
>
> >
> >
> > 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.
> >
>
> Don't do crazy monkey patching of queryset classes?
>
> Your query is quite expressible in the ORM:
>
> p_menu_ids =
Menu.objects.filter(parent_menu__isnull=False).order_by('parent_menu').distinct().values_list('parent_menu')
> qs = Menu.objects.exclude(id__in=p_menu_ids)
>
> Cheers
>
> Tom
>
> --
> 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.
>
--
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.