Sunday, April 22, 2012

Re: Modeling Question on Many-to-Many(with through) and Proxy models

Thanks, I saw that .model thing too, but was also unsure about it... I might just test it.

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> Should I create this many-to-many table as a normal model(no proxy) and set
> the foo foreingkey to Foo1 instead of Foo?
> Or should I use only FooBar as many-to-many and create some specific
> queries that would convert ALL the returned foo objects as Foo1 instance?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> Should I create this many-to-many table as a normal model(no proxy) and set
> the foo foreingkey to Foo1 instead of Foo?
> Or should I use only FooBar as many-to-many and create some specific
> queries that would convert ALL the returned foo objects as Foo1 instance?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> Should I create this many-to-many table as a normal model(no proxy) and set
> the foo foreingkey to Foo1 instead of Foo?
> Or should I use only FooBar as many-to-many and create some specific
> queries that would convert ALL the returned foo objects as Foo1 instance?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> Should I create this many-to-many table as a normal model(no proxy) and set
> the foo foreingkey to Foo1 instead of Foo?
> Or should I use only FooBar as many-to-many and create some specific
> queries that would convert ALL the returned foo objects as Foo1 instance?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> Should I create this many-to-many table as a normal model(no proxy) and set
> the foo foreingkey to Foo1 instead of Foo?
> Or should I use only FooBar as many-to-many and create some specific
> queries that would convert ALL the returned foo objects as Foo1 instance?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

--
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/-/TcNM3MbpT0gJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment